Reputation: 10697
df = pd.DataFrame(columns=['w','x','y','z'])
I'm trying to insert a new index row by row, and add values to certain columns.
If I were adding one value to a specific column, I could do: df.loc['a','x'] = 2
However what if I'd like to add values to several columns in one go, like this:
{'x':2, 'z':3}
is there a way to do this in pandas?
Upvotes: 0
Views: 1593
Reputation: 323316
df=pd.DataFrame(d,index=['a']).combine_first(df)
w x y z
a NaN 2 NaN 3
Upvotes: 2
Reputation: 294488
reindex
and assign
df.reindex(['a']).assign(**d)
w x y z
a NaN 2 NaN 3
Where:
d = {'x':2, 'z':3}
Upvotes: 2
Reputation: 4021
Use loc
but selecting multiple columns and assign an iterable (like a list or tuple)
df.loc['a',['x','z']] = [2,3]
Or as suggested from @jfaccioni, in case the data is a dictionary d
:
df.loc['a', list(d.keys())] = list(d.values())
Upvotes: 1