Plot dataset below using python datafram

Question:

I have dataset as below each row represent a customer ID and column is time and value for a month for every 30 min, I can’t plot that in time line graph

enter image description here

I want to convert it as below
enter image description here

any advise

trying……………

Asked By: Ahmed Raafat

||

Answers:

You can use pandas.wide_to_long to make this kind of reshape :

df.columns= df.columns.str.replace("s+", "", regex=True)

out = (
        pd.wide_to_long(df, stubnames=['Time', 'value'], i='ID',  j='_')
              .droplevel(1, axis=0)
              .reset_index()
              .sort_values(by='ID')
              .reset_index(drop=True)
      )

# Output :

print(out)

   ID            Time  value
0   1  1/12/2020 0:00      1
1   1  1/12/2020 0:30     10
2   1  1/12/2020 1:00     15
3   1  1/12/2020 1:30     20
4   1  1/12/2020 2:00      4
5   2  1/12/2020 0:00      3
6   2  1/12/2020 0:30      3
7   2  1/12/2020 1:00      4
8   2  1/12/2020 1:30      6
9   2  1/12/2020 2:00      5

Then, you can use matplotlib.pyplot.plot to get the desired plot :

import matplotlib.pyplot as plt 

plt.plot(out['Time'], out['value'])
plt.xticks(rotation='vertical')

enter image description here

Answered By: abokey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.