Home > Blockchain >  How to convert each column of a master DataFrame to seperate DataFrames?
How to convert each column of a master DataFrame to seperate DataFrames?

Time:10-01

I have a DataFrame which looks like the following (but with 40 columns):

import pandas as pd 
master_df = pd.DataFrame({'Date':pd.date_range('2018-01-01','2018-01-06'), 'A':[1,2,2,3,1,3], 'B':[10,10,20,30,'NaN',10], 'C':[1,3,'NaN',7,5,4]})

I would like to create a seperate DF for each column (with Date as index for each) with their own names, resulting in 40 DFs. Is there some iterative approach which I can use to do so?

CodePudding user response:

A comprehension dict might to the trick:

master_df.set_index('Date', inplace=True)
d = {col: master_df[[col]] for col in master_df.columns}

You then access each dataframe with a column as key e.g.:

d['A']

CodePudding user response:

Best is create dictionary of Series:

dfs = master_df.set_index('Date').to_dict('series')
print (dfs)
{'A': Date
2018-01-01    1
2018-01-02    2
2018-01-03    2
2018-01-04    3
2018-01-05    1
2018-01-06    3
Name: A, dtype: int64, 'B': Date
2018-01-01     10
2018-01-02     10
2018-01-03     20
2018-01-04     30
2018-01-05    NaN
2018-01-06     10
Name: B, dtype: object, 'C': Date
2018-01-01      1
2018-01-02      3
2018-01-03    NaN
2018-01-04      7
2018-01-05      5
2018-01-06      4
Name: C, dtype: object}

Or dictionary of one column DataFrames:

dfs = {k: v.to_frame() for k, v in master_df.set_index('Date').to_dict('series').items()}
print (dfs)
{'A':             A
Date         
2018-01-01  1
2018-01-02  2
2018-01-03  2
2018-01-04  3
2018-01-05  1
2018-01-06  3, 'B':               B
Date           
2018-01-01   10
2018-01-02   10
2018-01-03   20
2018-01-04   30
2018-01-05  NaN
2018-01-06   10, 'C':               C
Date           
2018-01-01    1
2018-01-02    3
2018-01-03  NaN
2018-01-04    7
2018-01-05    5
2018-01-06    4}

EDIT: It is not recommended, but possible :

for k, v in master_df.set_index('Date').to_dict('series').items():
    globals()[k] =  v.to_frame()
print (A)
            A
Date         
2018-01-01  1
2018-01-02  2
2018-01-03  2
2018-01-04  3
2018-01-05  1
2018-01-06  3
  • Related