这个问题类似于this one 但是,当我采用该解决方案时,会收到以下错误:
ValueError: cannot reindex from a duplicate axis
我正在尝试做这样的事情:
import pandas as pd
cols = {'foo': ['A','A','Z','A','Z'], 'bar' : [1,1,1,1,1]}
df = pd.DataFrame(data=cols)
df
bar foo
0 1 A
1 1 A
2 1 Z
3 1 A
4 1 Z
df[df['foo'] == 'Z']['bar'] = df[df['foo'] == 'Z']['bar'] * 100
C:\Anaconda3\envs\Scikit\lib\site-packages\ipykernel_launcher.py:1: SettingWithCopyWarning:
试图在DataFrame的切片副本上设置一个值。 尝试改用.loc [row_indexer,col_indexer] =值
请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy “”“启动IPython内核的入口点。
答案 0 :(得分:1)
使用DataFrame.loc
来按条件选择列:
df.loc[df['foo'] == 'Z', 'bar'] *= 100
#same like
#df.loc[df['foo'] == 'Z', 'bar'] = df.loc[df['foo'] == 'Z', 'bar'] * 100
print (df)
foo bar
0 A 1
1 A 1
2 Z 100
3 A 1
4 Z 100