[英]How to replace multiple column values for specific row in python dataframe?
I have dataframe with thousand columns, I want to replace a few columns (which I stored in a dictionary) for a specific row, how can I do that?我有 dataframe 和千列,我想为特定行替换几列(我存储在字典中),我该怎么做?
my_dict={'a':2,'b':1} #total feature is more than this.
Df.loc[i]=my_dict #i is row index
If you have another method to do without considering the dictionary then please suggest me, I will twerk my code accordingly.如果您有另一种方法可以不考虑字典,那么请建议我,我会相应地修改我的代码。 I just want to complete this operation.
我只想完成这个操作。
Say you have this input data:假设您有以下输入数据:
df = pd.DataFrame({'a':[1,2],'b':[3,4], 'c':[5,6]})
d = {'a':2,'b':1}
i = 0
Then you can do:然后你可以这样做:
df.loc[i,d.keys()] = d.values()
df
output: DF
df
:
a b c
0 2 1 5
1 2 4 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.