我想根据索引和列更改Pandas
DataFrame的值。我收到错误A value is trying to be set on a copy of a slice from a DataFrame
。我四处搜索并找到了类似的问题/答案,但没有一个我可以申请。
import pandas as pd
BabyDataSet = [('Bob', 968), ('Jessica', 155), ('Mary', 77), ('John', 578), ('Mel', 973)]
df = pd.DataFrame(index=['a','b','c','d','e'], data=BabyDataSet, columns=['Names', 'Births'])
df.ix['c']['Births'] = 10
print df
答案 0 :(得分:2)
你的语法错了应该是这样的:
In [30]:
df.ix['c','Births'] = 10
df
Out[30]:
Names Births
a Bob 968
b Jessica 155
c Mary 10
d John 578
e Mel 973
第一个组件是索引标签,第二个组件是列名。
您也可以
In [32]:
df.loc['c','Births'] = 10
df
Out[32]:
Names Births
a Bob 968
b Jessica 155
c Mary 10
d John 578
e Mel 973