Reputation: 39
I wasn't sure how to form this question, so the problem isn't really what it sounds for. Let's say I have a column with floats, ranging from 0.000000 to 1.000000 I want to reverse those values so for example:
1.000000 == 0.000000
0.122000 == 0.888000
0.950000 == 0.050000
0.324546 == 0.675454
How can I do it? I tried
normalized_df = normalized_df[headers[-1]].apply(lambda n: (1.000000 - n))
But I got an error with this block of code:
vals = normalized_df.values.tolist()
for e in vals:
del e[:3]
results = dict(zip(countries, vals))
An error:
Traceback (most recent call last):
File "...", line ..., in <module>
del e[:3]
TypeError: 'float' object does not support item deletion
This error normally doesn't happen without this code at the beginning of my question
Upvotes: 0
Views: 127
Reputation: 24322
Just simply substract 1 from your series:
s=pd.Series([1.0, 0.122, 0.95, 0.324546])
s=(1-s)
#here s is your Series
#If needed the difference as positive number use abs() method
s=(s-1).abs()
output:
0 0.000000
1 0.878000
2 0.050000
3 0.675454
Or
If you have df with int/float values:
df=pd.DataFrame({0: {0: 1.0, 1: 0.122, 2: 0.95, 3: 0.324546},
1: {0: 1.0, 1: 0.122, 2: 0.95, 3: 0.324546},
2: {0: 1.0, 1: 0.122, 2: 0.95, 3: 0.324546}})
df=1-df
#If needed the difference as positive number use abs() method
df.sub(1).abs()
output of df
:
0 1 2
0 0.000000 0.000000 0.000000
1 0.878000 0.878000 0.878000
2 0.050000 0.050000 0.050000
3 0.675454 0.675454 0.675454
Upvotes: 2