Analyst
Analyst

Reputation: 31

Creating Multiple DataFrames from single DataFrame based on different values of single column

I have 3 days of time series data with multiple columns in it. I have one single DataFrame which includes all 3 days data. I want 3 different DataFrames based on Column name "Dates" i.e df["Dates"]

For Example:

Available Dataframe is: df

enter image description here

Expected Output: Based on Three different Dates

First DataFrame: df_23

enter image description here

Second DataFrame: df_24

enter image description here

Third DataFrame: df_25

enter image description here

I want to use these all three DataFrames separately for analysis.

I tried below code but I am not able to use those three dataframes (Rather I don't know how to use.) Can anybody help me to work my code better. Thank you.

enter image description here

Above code is just printing the DataFrame in three DataFrames that too not as expected as per code!

enter image description here

Upvotes: 0

Views: 2485

Answers (1)

Umar.H
Umar.H

Reputation: 23099

Unsure if your saving your variable into a csv or keep it in memory for further use,

you could pass each unique value into a dict and access by it's value :

    print(df)
     Cal  Dates
0    85     23
1    75     23
2    74     23
3    97     23
4    54     24
5    10     24
6    77     24
7    95     24
8    58     25
9    53     25
10   44     25
11   94     25



d = {}

for frame, data in df.groupby('Dates'):
    d[f'df{frame}'] = data


print(d['df23'])
    Cal  Dates
0   85     23
1   75     23
2   74     23
3   97     23

edit updated request :

for k,v in d.items():
    i = (v['Cal'].loc[v['Cal'] > 70].count())
    print(f"{v['Dates'].unique()[0]} --> {i} times")
23 --> 4 times
24 --> 2 times
25 --> 1 times

Upvotes: 1

Related Questions