Home > Blockchain >  filter data based on time (HH:MM:SS)
filter data based on time (HH:MM:SS)

Time:10-06

I have below data set

Time Value
09:15:00 25
10:15:00 45
09:15:00 32
10:15:00 36
09:15:00 56
10:15:00 78

I would like to create a separate dataframe each based on the time

df0915:

Time Value
09:15:00 25
09:15:00 32
09:15:00 56

df1015:

Time Value
10:15:00 45
10:15:00 36
10:15:00 78

Any help?

CodePudding user response:

You can use pandas.DataFrame.groupby with a list comprehension.

out = [d for _, d in df.groupby('Time')]

# Output :

print(out)

[       Time  Value
 0  09:15:00     25
 2  09:15:00     32
 4  09:15:00     56,
        Time  Value
 1  10:15:00     45
 3  10:15:00     36
 5  10:15:00     78]

To access one of the dataframes, use out[0] or out[1], ....

CodePudding user response:

This will filter the dataframe:

df0915 = df[df['Time'] == '09:15:00']
df1015 = df[df['Time'] == '10:15:00']

print(df0915)
print(df1015)

Output:

       Time  Value
0  09:15:00     25
2  09:15:00     32
4  09:15:00     56
       Time  Value
1  10:15:00     45
3  10:15:00     36
5  10:15:00     78
  • Related