我想用熊猫系列或字典中的数据更新熊猫数据框中的某些单元格值.在后面的变量中,索引/键与数据框的列名匹配,但可以更少.例如:
import pandas as pd
import numpy as np
data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}
df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])
print df
Col1 Col2 Col3 Col4
R1 4 10 100 AAA
R2 5 20 50 BBB
R3 6 30 -30 AAA
R4 7 40 -50 CCC
series = pd.Series(data=[1, 2, 3, 4], index=['Col1', 'Col2', 'Col3', 'Col4'])
series_inverse = pd.Series(data=[1, 2, 3, 4], index=['Col4', 'Col3', 'Col2', 'Col1'])
series_imcomplete = pd.Series(data=[2, 3], index=['Col2', 'Col3'])
df.iloc[2] = series_imcomplete
print df
这使:
Col1 Col2 Col3 Col4
R1 4.0 10.0 100.0 AAA
R2 5.0 20.0 50.0 BBB
R3 NaN 2.0 3.0 NaN
R4 7.0 40.0 -50.0 CCC
此操作找到正确的列,但是如果条目较少,则将其替换为空值.所需的输出应为:
Col1 Col2 Col3 Col4
R1 4 10 100 AAA
R2 5 20 50 BBB
R3 6 2.0 3.0 AAA
R4 7 40 -50 CCC
我已经找到了使用替换的列解决方案,但是我无法使其适用于行.
我想知道是否有人可以给我一些建议.
解决方法:
尝试这个:
In [139]: df.loc[df.index[2], series_imcomplete.index] = series_imcomplete
In [140]: df
Out[140]:
Col1 Col2 Col3 Col4
R1 4 10 100 AAA
R2 5 20 50 BBB
R3 6 2 3 AAA
R4 7 40 -50 CCC
更新:从熊猫0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers开始.