我有一个pandas
DataFrame
有4列(时间,T1,T2,T3),并且使用定制的温度数据记录器在一天中获得它们各自的值。 Time
列的格式为[HH:MM:ss],但我想将其更改为[HH:MM](截断秒数)以进行绘图。有没有一种简单的方法来实现这一目标?
这是我的代码:(使用Python 3.6)
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(pd.read_excel('water_data_full_load.xlsx'))
df.drop("Date", axis = 1, inplace =True)
df.set_index('Time',inplace =True)
df.rename_axis({"T1": "Twall", "T2":"Twater", "T3":"Tsurr"}, axis=1, inplace=True)
df['Time'] = df['Time'].apply(lambda x: x[:5])
graph = df[['Twall','Twater','Tsurr']].plot()
plt.xticks(rotation =45)
plt.ylabel('Temperature ($^\circ$C)')
plt.xlabel('Time of the day (HH:MM)')
plt.show(graph)
答案 0 :(得分:0)
import pandas as pd
df = pd.DataFrame({ 'Time': ['22:05:00','22:06:00','22:07:00']})
df['Time_to_plot'] = df['Time'].apply(lambda x: x[:5])
print df
Time Time_to_plot
0 22:05:00 22:05
1 22:06:00 22:06
2 22:07:00 22:07
答案 1 :(得分:0)
<强>更新强>
In [41]: df = pd.DataFrame({'Time': ['11:07:00','12:06:00','13:17:00'], 'Twall':[10,20,30]}).set_index('Time')
In [42]: df
Out[42]:
Twall
Time
11:07:00 10
12:06:00 20
13:17:00 30
In [43]: df.index = df.index.astype(str).str.rsplit(':',n=1).str[0]
In [44]: df
Out[44]:
Twall
Time
11:07 10
12:06 20
13:17 30
演示(矢量化方法) - 用于截断datetime
dtype列中的秒数:
In [46]: df = pd.DataFrame(pd.date_range('2017-01-01', freq='99S', periods=10), columns=['date'])
In [47]: df
Out[47]:
date
0 2017-01-01 00:00:00
1 2017-01-01 00:01:39
2 2017-01-01 00:03:18
3 2017-01-01 00:04:57
4 2017-01-01 00:06:36
5 2017-01-01 00:08:15
6 2017-01-01 00:09:54
7 2017-01-01 00:11:33
8 2017-01-01 00:13:12
9 2017-01-01 00:14:51
In [49]: df['date'] = df['date'].values.astype('<M8[m]')
In [50]: df
Out[50]:
date
0 2017-01-01 00:00:00
1 2017-01-01 00:01:00
2 2017-01-01 00:03:00
3 2017-01-01 00:04:00
4 2017-01-01 00:06:00
5 2017-01-01 00:08:00
6 2017-01-01 00:09:00
7 2017-01-01 00:11:00
8 2017-01-01 00:13:00
9 2017-01-01 00:14:00