I have dataframe with column which has 2 values: good and bad.
I want to know if there is a way that I can swap them. For example whenever value is good should be changed to bad or vice versa. Is there a way to swap the values?
Edit: I want to swap values for certain indices after for loop. Example:
for i in np.arange(0,len(df)-1):
if(df.loc[i,'othercolumnname']>=100:
# swap the values here
CodePudding user response:
How about using Series.map
:
import pandas as pd
df = pd.DataFrame({'x': ['good', 'bad', 'bad', 'good']})
print(df)
df['x'] = df.x.map({'good': 'bad', 'bad': 'good'})
print(df)
x x
0 good 0 bad
1 bad → 1 good
2 bad 2 good
3 good 3 bad
For the edited part of the question, you can combine Series.map
and Series.where
:
import pandas as pd
df = pd.DataFrame({'x': ['good', 'bad', 'bad', 'good'], 'y': [100, 0, 100, 0]})
print(df)
df['x'] = df.x.map({'good': 'bad', 'bad': 'good'}).where(df.y >= 100, df.x)
print(df)
The idea is to swap the values globally (using map
) and then revert back (using where
) to the original x
where the condition df.y >= 100
is False
.
Alternatively, you can use DataFrame.apply
:
swap = {'good': 'bad', 'bad': 'good'}
df['x'] = df.apply(lambda row: swap[row.x] if row.y >= 100 else row.x, axis=1)
x y x y
0 good 100 0 bad 100
1 bad 0 → 1 bad 0
2 bad 100 2 good 100
3 good 0 3 good 0
CodePudding user response:
You can just use the pandas
function replace
that can make dict-like replacements.
df.replace({'YourColumnName': {'good': 'bad',
'bad': 'good'}})
CodePudding user response:
You can use replace
to achieve this...
df = pd.DataFrame({'A': ["good", "good", "bad", "bad", "good"],
'B': [5, 6, 7, 8, 9],
'C': ['a', 'b', 'c', 'd', 'e'],
'D': ["good", "good", "bad", "bad", "good"],})
print(df.replace({'A': {"good": "bad", "bad": "good"}}))
This will limit to only column A
. If you want all columns, you can unnest the dictionary in replace. and just pass {"good": "bad", "bad": "good"}
CodePudding user response:
Using np.where
and @j1-lee's dataframe:
import numpy as np
df = pd.DataFrame({'x': ['good', 'bad', 'bad', 'good']})
df['y'] = np.where(df['x'].eq('good'),'bad','good')
x y
0 good bad
1 bad good
2 bad good
3 good bad
CodePudding user response:
Here's a trick that will work as expected for two unique values:
df['x'].replace({k: v for k, v in zip(df['x'].unique(), df['x'].unique()[::-1])})
Output:
>>> df
x
0 good
1 bad
2 bad
3 good
>>> df['x'] = df['x'].replace({k: v for k, v in zip(df['x'].unique(), df['x'].unique()[::-1])})
>>> df
0 bad
1 good
2 good
3 bad