将pandas数据帧中的两列合并到lis中

2024-05-26 19:54:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将两个数据帧合并到一个新的数据帧中,其中两个列将合并为一个列表。例如:这是df1

   tkt_ticket_opened  tkt_adjtimetorepair  result_data_cohort_id
0      2017-01-09 05             0.075883                      1
1      2017-01-09 06             0.286550                      1
2      2017-01-09 07             0.124234                      1
3      2017-01-09 08             0.144504                      1
4      2017-01-09 09             0.416698                      1
5      2017-01-09 10             0.103199                      1
6      2017-01-09 11             0.063608                      1
7      2017-01-09 12             0.378695                      1
8      2017-01-09 13             0.686515                      1
9      2017-01-09 14             0.671016                      1
10     2017-01-09 15             0.406588                      1
11     2017-01-09 16             0.957627                      1
12     2017-01-09 17             0.504509                      1
13     2017-01-09 18             0.416487                      1
14     2017-01-09 19             0.412306                      1
15     2017-01-09 20             0.929061                      1
16     2017-01-09 21             0.421006                      1
17     2017-01-09 22             0.365754                      1
18     2017-01-09 23             0.557050                      1

df2型

^{pr2}$

产生的数据帧将是

   tkt_ticket_opened  tkt_adjtimetorepair  
0      2017-01-09 05             [0.075883, 0.065538 ]                 
1      2017-01-09 06             [0.286550, 0.379438 ]                 
2      2017-01-09 07             [0.124234, 0.190816 ]                   
....

如果有任何帮助,我们将不胜感激。在


Tags: 数据id列表dataresultticketcohortdf1
2条回答

选项:<1>

df_a = pd.DataFrame([[1, 3], [2, 3], [3, 3]], columns=["tkt_ticket_opened", "tkt_adjtimetorepair"])
df_b = pd.DataFrame([[1, 4], [2, 4], [3, 4]], columns=["tkt_ticket_opened", "tkt_adjtimetorepair"])

组合数据的一种方法是根据您期望的结果构造序列。使用数据帧的简化版本,可以将列压缩到一起以生成所需的结果:

^{pr2}$

选项2:

通过合并所需键上的数据帧,然后将两列发送到列表中,也可以获得相同的结果:

df_c = pd.merge(df_a, df_b, on="tkt_ticket_opened")
df_c["tkt_adjtimetorepair"] = df_c[["tkt_adjtimetorepair_x", "tkt_adjtimetorepair_y"]].values.tolist()
df_c = df_c[["tkt_ticket_opened", "tkt_adjtimetorepair"]]

df_c.head()

     tkt_ticket_opened   tkt_adjtimetorepair

0         1                  [3, 4]

1         2                  [3, 4]

2         3                  [3, 4]

我更喜欢选项2,因为这是更有效和更好的熊猫解决方案。在

首先,合并数据集:

merged = pd.merge(df1, df2, on= 'tkt_ticket_opened')

接下来,我们将获取包含tkt_adjtimetorepair列值的数组并将它们转换为列表:

^{pr2}$

我们可以将这个输出直接分配给一个列。在

相关问题 更多 >

    热门问题