Reputation: 71
I am trying to change the values of a data frame based on different conditions in python.
Here is an example of my dataset
df1=pd.DataFrame(np.array([['A',1, 2, 'NaN'], ['B',-4, 5, -6], ['C',7, 0, 9]]), columns=['Details','a', 'b', 'c'])
and i want to change the values as follwoing,
df1=pd.DataFrame(np.array([['A','big', 'big', 'No data'], ['B','small', 'big', 'small'], ['C','big', 'zero', 'big']]),columns=['Details','a', 'b', 'c'])
I have tried using the following line but this did not work. I am wondering if there is any easy way to do this. thanks in advance
df1[df1.columns[1:]] = df1.iloc[:,1:].where(df1.iloc[:,1:] >0 ,'big')
Upvotes: 2
Views: 69
Reputation: 863166
Use numpy.select
only for selected columns:
c = ['a','b','c']
#if necessary convert to numeric
df1[c] = df1[c].astype(float)
df1[c] = np.select([df1[c] > 0, df1[c] <= 0], ['big','small'], default='No data')
print (df1)
Details a b c
0 A big big No data
1 B small big small
2 C big small big
If need test all columns without first:
df1.iloc[:, 1:] = np.select([df1.iloc[:, 1:] > 0,
df1.iloc[:, 1:] <= 0], ['big','small'], default='No data')
Upvotes: 5