简体   繁体   中英

Split rows in a column and plot graph for a dataframe. Python

My data set contains the data of days and hrs

time slot              hr_slot       location_point
2019-01-21 00:00:00       0              34
2019-01-21 01:00:00       1              564
2019-01-21 02:00:00       2              448 
2019-01-21 03:00:00       3              46
.
.
.
.
2019-01-22 23:00:00       23             78
2019-01-22 00:00:00       0              34
2019-01-22 01:00:00       1              165
2019-01-22 02:00:00       2              65 
2019-01-22 03:00:00       3              156
.
.
.
.
2019-01-22 23:00:00       23             78

The data set conatins 7 days. that is 7*24 row. How to plot the graph for the dataset above.

hr_slot on the X axis : (0-23 hours)
loaction_point on Y axis : (location_point)
and each day should have different color on the graph: (Day1: color1, Day2:color2....)

Consider pivoting your data first:

# Create normalized date column
df['date'] = df['time slot'].dt.date.astype(str)

# Pivot
piv = df.pivot(index='hr_slot', columns='date', values='location_point')
piv.plot()

Update

To filter which dates are plotted, using loc or iloc :

# Exclude first and last day    
piv.iloc[:, 1:-1].plot()

# Include specific dates only
piv.loc[:, ['2019-01-21', '2019-01-22']].plot()

Alternate approach using pandas.crosstab instead:

(pd.crosstab(df['hr_slot'],
             df['time slot'].dt.date,
             values=df['location_point'],
             aggfunc='sum')
 .plot())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM