简体   繁体   English

Pivot 矩阵到时间序列 - Python

[英]Pivot matrix to time-series - Python

I've got a dataframe with date as first column and time as the name of the other columns.我有一个 dataframe 日期作为第一列,时间作为其他列的名称。

Date日期 13:00 13:00 14:00 14:00 15:00 15:00 16:00 16:00 ... ...
2022-01-01 2022-01-01 B R R M M ... ...
2022-01-02 2022-01-02 B B B M ... ...
2022-01-03 2022-01-03 R R B B M ... ...

How could I transform that matrix into a datetime time-series?如何将该矩阵转换为日期时间时间序列? My objective its something like this:我的目标是这样的:

Date日期 Data数据
2022-01-01 13:00 2022-01-01 13:00 B
2022-01-01 14:00 2022-01-01 14:00 R R
2022-01-01 15:00 2022-01-01 15:00 M
2022-01-01 16:00 2022-01-01 16:00 M
... ... ... ...

I think it could be done using pivot.我认为可以使用 pivot 来完成。 I would really appreciate any help you could give me.我真的很感激你能给我的任何帮助。 Thanks in advance!!提前致谢!!

Try .set_index / .stack .尝试.set_index / .stack The rest is just convert the string to DateTime: rest 只是将字符串转换为日期时间:

df = df.set_index("Date").stack().reset_index()
df["Date"] = pd.to_datetime(df["Date"] + " " + df["level_1"])
df = df.rename(columns={0: "Data"})
print(df[["Date", "Data"]])

Prints:印刷:

                  Date Data
0  2022-01-01 13:00:00    B
1  2022-01-01 14:00:00    R
2  2022-01-01 15:00:00    M
3  2022-01-01 16:00:00    M
4  2022-01-02 13:00:00    B
5  2022-01-02 14:00:00    B
6  2022-01-02 15:00:00    B
7  2022-01-02 16:00:00    M
8  2022-01-03 13:00:00    R
9  2022-01-03 14:00:00    B
10 2022-01-03 15:00:00    B
11 2022-01-03 16:00:00    M

An alternative:替代:

df = pd.DataFrame({'Date': ['2022-01-01', '2022-01-02', '2022-01-03'], '13:00': ['B', 'B', 'R'], '14:00': ['R', 'B', 'R'], '15:00': ['M', 'B', 'B'], '16:00': ['M', 'M', 'M']})
df = df.melt(id_vars='Date', var_name='Time', value_name='Data')
df['Date'] = df['Date'] + ' ' + df['Time']
df = df[['Date', 'Data']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM