在Pandas中将数据框透视,同时创建额外的新列以存储数值。

huangapple go评论65阅读模式
英文:

Pivot a dataframe in pandas while creating additional new columns for values

问题

ID 生效日期 名字 姓氏 出生日期 状态 开始日期 结束日期
MO12 1/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 2/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 3/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 4/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022

请注意,你提供的代码可能需要一些修改,以便正确执行所需的数据透视操作。但在转换后的数据表格中,已经按照你的要求将日期列转换为名为"状态"的列,并且包含了相应的日期行。

英文:

I have a table that looks like this:

ID First Name Last Name Date of Birth 1/1/2022 2/1/2022 3/1/2022 4/1/2022 Start Date End Date
MO12 Wanda Sample 1/1/2020 Good Good Good Good 1/1/2022 1/31/2022

I'd like to pivot the data to look like this:

ID Effective Date First Name Last Name Date of Birth Status Start Date End Date
MO12 1/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 2/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 3/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022
MO12 4/1/2022 Wanda Sample 1/1/2020 Good 1/1/2022 1/31/2022

Effectively, this would turn the individually dated columns into a column called "Status" and rows with the individual dates.

I started with this basic code, but I'm getting mixed up in the syntax to create the new columns and assign the values to their respective columns.

import pandas as pd
import numpy as np

df = pd.read_csv(r'C:\Users\User\sample.csv')
df.pivot_table(columns=['Effective Date','First Name','Last Name','Date of Birth','Status','Start Date','End Date'],values=['1/1/2022','2/1/2022','3/1/2022','4/1/2022'], aggfunc=np.max)
df.head

答案1

得分: 2

尝试使用 melt 函数:

df.melt(['ID', 'First Name', 'Last Name', 'Date of Birth', 'Start Date', 'End Date'], 
        var_name='Effective Date', value_name='Status')

输出:

     ID First Name Last Name Date of Birth Start Date   End Date Effective Date Status
0  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       1/1/2022   Good
1  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       2/1/2022   Good
2  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       3/1/2022   Good
3  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       4/1/2022   Good
英文:

Try melt:

df.melt(['ID', 'First Name', 'Last Name', 'Date of Birth', 'Start Date', 'End Date'], 
        var_name='Effective Date', value_name='Status')

Output:

     ID First Name Last Name Date of Birth Start Date   End Date Effective Date Status
0  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       1/1/2022   Good
1  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       2/1/2022   Good
2  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       3/1/2022   Good
3  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       4/1/2022   Good

答案2

得分: 2

你可以尝试使用.set_index() + .stack() + .reset_index()。然后只需重命名列名:

df = df.set_index(['ID', 'First Name', 'Last Name', 'Date of Birth', 'Start Date', 'End Date']).stack().reset_index(name='Status').rename(columns={'level_6': 'Effective Date'})
print(df)

打印结果:

     ID First Name Last Name Date of Birth Start Date   End Date Effective Date Status
0  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       1/1/2022   Good
1  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       2/1/2022   Good
2  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       3/1/2022   Good
3  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       4/1/2022   Good
英文:

You can try to .set_index() + .stack() + .reset_index(). Then just rename columns:

df = df.set_index(['ID', 'First Name', 'Last Name', 'Date of Birth', 'Start Date', 'End Date']).stack().reset_index(name='Status').rename(columns={'level_6': 'Effective Date'})
print(df)

Prints:

     ID First Name Last Name Date of Birth Start Date   End Date Effective Date Status
0  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       1/1/2022   Good
1  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       2/1/2022   Good
2  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       3/1/2022   Good
3  MO12      Wanda    Sample      1/1/2020   1/1/2022  1/31/2022       4/1/2022   Good

huangapple
  • 本文由 发表于 2023年7月12日 23:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定