gpt4 book ai didi

python - reshape DataFrame 并根据其他 24 列修改一列

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:59 25 4
gpt4 key购买 nike

我有一个有 26 列的 Excel。

日期唯一 IDH01H02H03.. .H24

此处 H{n} 代表小时,即对于 19/7/2017 01.00.00 的 UID some_code,其值为 19919/7/2017 02.00.00 的值为 7 等。

+--------------------+---------------+----------+---------------+
| Date | UID | H01 | H02 |
+--------------------+---------------+----------+---------------+
| 19/7/2017 00.00.00 | some_code | 199 | 7 |
| 19/7/2017 00.00.00 | another_code | 164 | 18 |
| 19/7/2017 00.00.00 | new_code | 209 | 1 |
| 19/7/2017 00.00.00 | code_5 | 85 | 4 |
| 19/7/2017 00.00.00 | what | 45 | 6 |

我正在阅读 Excel 并创建一个与上面类似的 DataFrame。

我想修改这个 DataFrame,以便得到下面的结果。

+--------------------+---------------+----------+
| Date | UID | Value |
+--------------------+---------------+----------+
| 19/7/2017 01.00.00 | some_code | 199 |
| 19/7/2017 02.00.00 | some_code | 7 |
| 19/7/2017 03.00.00 | some_code | ... |
.................................................
.................................................
| 19/7/2017 00.00.00 | some_code | ... |
| 19/7/2017 01.00.00 | another_code | 164 |
| 19/7/2017 02.00.00 | another_code | 18 |
| 19/7/2017 03.00.00 | another_code | ...|
.................................................
.................................................
| 19/7/2017 00.00.00 | another_code | ...|

我是 Python 和 Pandas 新手,无法理解 stack/unstack/pivot。

最佳答案

您可以使用:

<小时/>
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y %H.%M.%S')
df = df.set_index(['Date','UID'])
df.columns=pd.to_timedelta(df.columns.str.extract('(\d+)',expand=False).astype(int),unit='H')
df = df.stack().reset_index(name='Value')
df['Date'] = df['Date'] + df['level_2']
df = df.drop('level_2', axis=1)
print (df)
Date UID Value
0 2017-07-19 01:00:00 some_code 199
1 2017-07-19 02:00:00 some_code 7
2 2017-07-19 01:00:00 another_code 164
3 2017-07-19 02:00:00 another_code 18
4 2017-07-19 01:00:00 new_code 209
5 2017-07-19 02:00:00 new_code 1
6 2017-07-19 01:00:00 code_5 85
7 2017-07-19 02:00:00 code_5 4
8 2017-07-19 01:00:00 what 45
9 2017-07-19 02:00:00 what 6

对于相同格式的日期,请添加 dt.strftime :

...
df['Date'] = (df['Date'] + df['level_2']).dt.strftime('%d/%m/%Y %H.%M.%S')
df = df.drop('level_2', axis=1)
print (df)
Date UID Value
0 19/07/2017 01.00.00 some_code 199
1 19/07/2017 02.00.00 some_code 7
2 19/07/2017 01.00.00 another_code 164
3 19/07/2017 02.00.00 another_code 18
4 19/07/2017 01.00.00 new_code 209
5 19/07/2017 02.00.00 new_code 1
6 19/07/2017 01.00.00 code_5 85
7 19/07/2017 02.00.00 code_5 4
8 19/07/2017 01.00.00 what 45
9 19/07/2017 02.00.00 what 6

关于python - reshape DataFrame 并根据其他 24 列修改一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com