简体   繁体   中英

pandas: fill multiple empty dataframes

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.

Use a for loop:

for df in (variables, Q1):
    df.fillna(0, inplace=True)

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')

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}

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