2

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.

3 Answers 3

2

Use a for loop:

for df in (variables, Q1):
    df.fillna(0, inplace=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Would this require the inplace=True parameter?
1

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

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.