Reputation: 1684
I'm declaring multiple empty dataframes as follows:
variables = pd.DataFrame(index=range(10),
columns=['P1', 'P2', 'P3'],
dtype='float64')
Q1 = pd.DataFrame(index=range(10),
columns=['P1H1', 'P1H2'],
dtype='float64')
I can use fillna as follows:
variables = variables.fillna(0)
Q1 = Q1.fillna(0)
What is a more pythonic way of filling multiple dataframes simultaneously ?
Reason: Here I have given only two dataframes, however, the real problem has many more dataframes, which I have to update periodically.
Upvotes: 2
Views: 189
Reputation: 109726
Given that your dtype and index is the same, you can use a dictionary comprehension where each dataframe is a value in the dictionary.
cols = {'variables': ['P1', 'P2', 'P3'],
'Q1': ['P1H1', 'P1H2']}
dfs = {key: pd.DataFrame(index=range(10), columns=cols[key], dtype='float64').fillna(0)
for key in cols}
Upvotes: 0
Reputation: 863531
Maybe you can fill columns in DataFrame contructor
with 0
, then fillna
can be omited :
import pandas as pd
variables = pd.DataFrame(index=range(10),
columns=['P1', 'P2', 'P3'],
data={'P1':[0],'P2':[0],'P3':[0]},
dtype='float64')
print variables
P1 P2 P3
0 0.0 0.0 0.0
1 0.0 0.0 0.0
2 0.0 0.0 0.0
3 0.0 0.0 0.0
4 0.0 0.0 0.0
5 0.0 0.0 0.0
6 0.0 0.0 0.0
7 0.0 0.0 0.0
8 0.0 0.0 0.0
9 0.0 0.0 0.0
Q1 = pd.DataFrame(index=range(10),
columns=['P1H1', 'P1H2'],
data={'P1H1':[0],'P1H2':[0]},
dtype='float64')
print Q1
P1H1 P1H2
0 0.0 0.0
1 0.0 0.0
2 0.0 0.0
3 0.0 0.0
4 0.0 0.0
5 0.0 0.0
6 0.0 0.0
7 0.0 0.0
8 0.0 0.0
9 0.0 0.0
Also, parameter columns
can be omited:
import pandas as pd
variables = pd.DataFrame(index=range(10),
data={'P1':[0],'P2':[0],'P3':[0]},
dtype='float64')
Q1 = pd.DataFrame(index=range(10),
data={'P1H1':[0],'P1H2':[0]},
dtype='float64')
Upvotes: 1
Reputation: 3991
Use a for
loop:
for df in (variables, Q1):
df.fillna(0, inplace=True)
Upvotes: 2