あなたはその後、hour
Sを追加to_timedelta
に変換し、最後の削除列A
drop
によっておよびDate
列でsort_values
必要に応じて、dict
によってlreshape
を必要とする:別の解決策は、str.extract
でMultiIndex.from_arrays
を作成し、DataFrame.stack
によって再構築され
print (df)
Date hour1 value1 hour2 value2 hour24 value24
0 2016-01-01 1 4100 2 3500 24 5200
1 2016-01-02 1 3000 2 3700 24 7200
a = [col for col in df.columns if col.startswith('hour')]
b = [col for col in df.columns if col.startswith('value')]
df = pd.lreshape(df, {'A' : a, 'B' : b})
df['Date'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['A'], unit='h')
df = df.drop('A', axis=1).sort_values('Date')
print (df)
Date B
0 2016-01-01 01:00:00 4100
2 2016-01-01 02:00:00 3500
4 2016-01-02 00:00:00 5200
1 2016-01-02 01:00:00 3000
3 2016-01-02 02:00:00 3700
5 2016-01-03 00:00:00 7200
:
df = df.set_index('Date')
mux = df.columns.to_series().str.extract('([A-Za-z]+)(\d+)', expand=True)
df.columns = pd.MultiIndex.from_arrays([mux[0], mux[1]], names=('a','b'))
df = df.stack(1).reset_index()
df['Date'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df['hour'], unit='h')
df = df.drop(['b', 'hour'], axis=1).rename_axis(None, axis=1)
print (df)
Date value
0 2016-01-01 01:00:00 4100
1 2016-01-01 02:00:00 3500
2 2016-01-02 00:00:00 5200
3 2016-01-02 01:00:00 3000
4 2016-01-02 02:00:00 3700
5 2016-01-03 00:00:00 7200