简体   繁体   English

如何替换 python dataframe 中特定行的多个列值?

[英]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.

相关问题 如何用另一个 dataframe 中的值替换多列和特定行中的 0 - How to replace 0 in multiple columns and specific row with values from another dataframe 如何在特定列和行上替换 dataframe 中的字符串? - how to replace string in dataframe on specific column and row? Python如何在数据框中替换列的值 - Python how to replace a column's values in dataframe 根据 python dataframe 中的特定条件减去特定列的行值 - Subtracting values of a row for a specific column based on a specific condition in python dataframe 如何替换熊猫数据框中特定列中的特定值 - How to replace specific values in a specific column in a pandas dataframe Python Dataframe 替换由逗号单行分隔的多个值 - Python Dataframe replace multiple values that separated by comma single row 如何用 python 中的特定值替换列的值? - How replace values of column with specific value in python? 如何通过在 python DataFrame 中保持其他值不变来替换特定列上的字符串值 - How to replace a string value on a specific column by keeping other values unchanged in python DataFrame 在 Python 的 dataframe 的行尾添加特定的列值 - Add a specific column values at the end of row in dataframe in Python 如何替换熊猫数据框中列的选定行的值? - how to replace values of selected row of a column in panda's dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM