Plot dataset below using python datafram
Question:
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')
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')