在以下日期之前,不要将日期转换为字符串
pivot_table
,所以可以很容易地做到
rename
:
df['Date'] = pd.to_datetime(df['Date'])
df['is'] = df['Band_In'] / df['Speed'] * 100
out = (df.pivot_table(index=['Node', 'Interface', 'carrier'],
columns='Date', values='is')
.rename(columns=lambda x: x.strftime('%-d-%b'))
.reset_index().rename_axis(columns=None)
)
输出:
Node Interface carrier 1-May 9-May 2-Jun 21-Jun 1-Jul 19-Jul
0 Server1 wan1 ATT NaN 80.0 NaN NaN NaN NaN
1 Server1 wan1 Sprint NaN NaN NaN 50.0 NaN NaN
2 Server1 wan1 Verizon NaN NaN NaN NaN 30.0 NaN
3 Server2 wan1 ATT 90.0 NaN NaN NaN NaN NaN
4 Server2 wan1 Sprint NaN NaN 88.0 NaN NaN NaN
5 Server2 wan1 Verizon NaN NaN NaN NaN NaN 22.0