Many ways to do that
1
In [7]: d.sales[d.sales==24] = 100
In [8]: d
Out[8]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 12 2008
2 sat banana 22 2008
3 sun banana 23 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 23 2009
7 sun banana 100 2009
2
In [26]: d.loc[d.sales == 12, 'sales'] = 99
In [27]: d
Out[27]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 99 2008
2 sat banana 22 2008
3 sun banana 23 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 23 2009
7 sun banana 100 2009
3
In [28]: d.sales = d.sales.replace(23, 24)
In [29]: d
Out[29]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 99 2008
2 sat banana 22 2008
3 sun banana 24 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 24 2009
7 sun banana 100 2009
Answer from waitingkuo on Stack OverflowMany ways to do that
1
In [7]: d.sales[d.sales==24] = 100
In [8]: d
Out[8]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 12 2008
2 sat banana 22 2008
3 sun banana 23 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 23 2009
7 sun banana 100 2009
2
In [26]: d.loc[d.sales == 12, 'sales'] = 99
In [27]: d
Out[27]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 99 2008
2 sat banana 22 2008
3 sun banana 23 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 23 2009
7 sun banana 100 2009
3
In [28]: d.sales = d.sales.replace(23, 24)
In [29]: d
Out[29]:
day flavour sales year
0 sat strawberry 10 2008
1 sun strawberry 99 2008
2 sat banana 22 2008
3 sun banana 24 2008
4 sat strawberry 11 2009
5 sun strawberry 13 2009
6 sat banana 24 2009
7 sun banana 100 2009
Not sure about older version of pandas, but in 0.16 the value of a particular cell can be set based on multiple column values.
Extending the answer provided by @waitingkuo, the same operation can also be done based on values of multiple columns.
d.loc[(d.day== 'sun') & (d.flavour== 'banana') & (d.year== 2009),'sales'] = 100
pandas - Conditionally replace dataframe cells with value from another cell - Data Science Stack Exchange
Pandas: replace single cell in data frame: variable assignment not in place? what can I do?
I need to replace NaN in one column with value for other col
[Pandas] Fill empty cells in column with value of other columns
Hi there,
I have a titanic data set with unclean entries. I want to change the sex from each entry to 'm' for male and 'f' for female.
The input looks like this:
107,1,3,"Salkjelsvik, Miss. Anna Kristine",1,21,0,0,343120,7.65,,S 108,1,3,"Moss, Mr. Albert Johan",-1,,0,0,312991,7.775,,S 109,0,3,"Rekic, Mr. Tido",-1,38,0,0,349249,7.8958,,S 110,1,3,"Moran, Miss. Bertha",1,,1,0,371110,24.15,,Q 111,0,1,"Porter, Mr. Walter Chamberlain",-1,47,0,0,110465,52,C110,S 112,0,3,"Zabour, Miss. Hileni",female,14.5,1,0,2665,14.4542,,C 113,0,3,"Barton, Mr. David John",male,22,0,0,324669,8.05,,S
I tried
import pandas as pd
data = pd.read_csv('titanicClean.csv')
for i in range(len(data)):
if data.iloc[i].Sex == 'f' or data.iloc[i].Sex == 'm':
continue
else:
if 'f' in data.iloc[i].Sex or data.iloc[i].Sex == '1':
data.iloc[i].Sex = 'f'
else:
data.iloc[i].Sex = 'm'
yet either it doesn't change the values or it doesn't do it inplace. I also tried data.iloc[i].Sex.replace(data.iloc[i].Sex, 'f', inplace = True) yet here it complains that the function doesn't accept keywords and data[data.Sex == data.iloc[i].Sex] = 'm' which has the same problem as above.
Is there a way to replace single cell values in pandas by variable assignment or do I need a special function for this?