0

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

Analyst
  • 31
  • 8
  • Possible duplicate of [Python - splitting dataframe into multiple dataframes based on column values and naming them with those values](https://stackoverflow.com/questions/40498463/python-splitting-dataframe-into-multiple-dataframes-based-on-column-values-and) – Umar.H Oct 16 '19 at 22:08
  • @Datanovice: I tried using the similar code but I am not getting the expected output! – Analyst Oct 17 '19 at 14:39
  • 2
    [You should not post code as an image because...](https://meta.stackoverflow.com/a/285557/1422451) – Parfait Oct 17 '19 at 14:49
  • @Parfait: Sure! I don't know about that. Thanks for suggestion. – Analyst Oct 17 '19 at 16:04

1 Answers1

1

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
Umar.H
  • 22,559
  • 7
  • 39
  • 74
  • If I want to iterate through Index values and dictionary (d--> dictionary of df) How can I do that? For Example: If I want to count how many times 'cal' column values are exceeding '70' per day! Expected output from your dataframe should be: on 23 --> 4 Times, on 24--> 2 times and on25--> 1Time – Analyst Oct 17 '19 at 16:14
  • @Analyst updated for you - but this is basic for looping and working with python data types, I suggest you look at some basic tutorials for python and maybe search SO a little :) ask a new question if things get to complex but always post your data as text, show your code and paste any errors. – Umar.H Oct 17 '19 at 16:24
  • 1
    Yeah Thanks. I will do that from next for sure. I am trying out. I will post my code and error too for reference as well. Thanks for suggestions. Appreciate for that. – Analyst Oct 17 '19 at 16:49