简体   繁体   中英

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?

    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:


    a   b   c
0   2   1   5
1   2   4   6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM