I have a pandas dataframe like so:
import pandas as pd
df = pd.DataFrame({
'id':[1,2,3,4,5,6],
'a':[1,2,3,4,5,6],
'b':['a', 'b', 'c', 'd', 'e', 'f']
})
And I would like to replace values in columns a and b with constants given by a dictionary like so:
fills = dict(
a = 1,
b = 'a'
)
to obtain a result like this:
id a b
0 1 1 a
1 2 1 a
2 3 1 a
3 4 1 a
4 5 1 a
5 6 1 a
Obviously, I can do:
for column in fills:
df.loc[:, column] = fills[column]
To get the desired results of:
id a b
0 1 1 a
1 2 1 a
2 3 1 a
3 4 1 a
4 5 1 a
5 6 1 a
But is there perhaps some pandas function, that would let me pass the dictionary as an argument and to this replacement without writing a python loop?