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?
JavaScript
x
3
1
my_dict={'a':2,'b':1} #total feature is more than this.
2
Df.loc[i]=my_dict #i is row index
3
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.
Advertisement
Answer
Say you have this input data:
JavaScript
1
4
1
df = pd.DataFrame({'a':[1,2],'b':[3,4], 'c':[5,6]})
2
d = {'a':2,'b':1}
3
i = 0
4
Then you can do:
JavaScript
1
2
1
df.loc[i,d.keys()] = d.values()
2
df
output:
JavaScript
1
4
1
a b c
2
0 2 1 5
3
1 2 4 6
4