Hamideh
Hamideh

Reputation: 697

How can I capture the data frames from a list in Python Pandas?

I want to combine data frames with different columns in Python Pandas.

import pandas as pd 

first_df = pd.DataFrame({ 
    'a':np.random.randn(6),
    'b':np.random.choice( [5,7,np.nan], 6),
    'c':np.random.choice( ['panda','python','shark'], 6)})

second_df = pd.DataFrame({ 
    'a':np.random.randn(6),
    'b':np.random.choice( [5,7,np.nan], 6)})

third_df = pd.DataFrame({ 
    'a':np.random.randn(6)})

I got a list of dataframes

df_list = %who_ls DataFrame 
print(len(df_list))
df_list

but I do not know how to capture the dataframes objects themselves so that I can combine them:

pd.concat(df_list, axis=0, ignore_index=True)

Upvotes: 2

Views: 117

Answers (2)

Hamza usman ghani
Hamza usman ghani

Reputation: 2243

You can get list of all dfs stored in your memory using:

df_list = [var for var in dir() if isinstance(eval(var), pd.core.frame.DataFrame)]
print(df_list)
>> ['first_df', 'second_df', 'third_df']

Upvotes: 0

aSaffary
aSaffary

Reputation: 863

i'm not sure why you use who_ls, if you want to combine dataframes just make a list of them like this:

df = pd.concat([first_df, second_df, third_df])

edit: i strongly suggest you avoid using who_ls and globals() to do what you want, there are a couple of things that could go wrong if you change your code in the futrue, plus it's not much readable. but it will work as someone already suggested in the comments. but please consider making a list of objects as you load/create them to avoid this.

Upvotes: 1

Related Questions