如何将 pandas 中的两行合并为一行?

发布于 2025-01-15 06:29:17 字数 1346 浏览 2 评论 0原文

我有这个 DataFrame:

    id   type    value
0  104     0       7999
1  105     1  196193579
2  108     0     245744
3  108     1   93310128

我需要合并具有相同 id 的行并将两个值保留在同一行中,以下示例是我所需要的:

    id   type    value_0     value_1
0  104     0       7999       0
1  105     1        0      196193579
2  108     0     245744    93310128

我有以下代码,用于分组并更改每行的值

 def concat_rows(self, rows ):
        row = rows.iloc[0]

        if len(rows) > 1:
            row1 = rows.iloc[0]
            row2 = rows.iloc[1]
            row['value_1'] = row1['value'] if row1['type'] == 1 else row2['value']
            row['value_0'] = row1['value'] if row1['type'] == 0 else row2['value']
        else:

            row['value_1'] = row['value'] if row['type'] == 1 else 0
            row['value_0'] = row['value'] if row['type'] == 0 else 0
        return row

df2 = df.groupby('id').apply(self.concat_rows).reset_index(drop=True)

但是我得到了下表,其中包含修改后的数字

    id  value    type  value_1  value_0
0  104   7999     0        0     7999
1  105     99     1       99      399
2  108  10770     0    12118    10770

数据:

{'id': [104, 105, 108, 108],
 'type': [0, 1, 0, 1],
 'value': [7999, 196193579, 245744, 93310128]}

I have this DataFrame:

    id   type    value
0  104     0       7999
1  105     1  196193579
2  108     0     245744
3  108     1   93310128

I need to merge rows that have the same id and keep the two values in the same row, the following example is what I require:

    id   type    value_0     value_1
0  104     0       7999       0
1  105     1        0      196193579
2  108     0     245744    93310128

I have the following code, with which to group and change the values for each row

 def concat_rows(self, rows ):
        row = rows.iloc[0]

        if len(rows) > 1:
            row1 = rows.iloc[0]
            row2 = rows.iloc[1]
            row['value_1'] = row1['value'] if row1['type'] == 1 else row2['value']
            row['value_0'] = row1['value'] if row1['type'] == 0 else row2['value']
        else:

            row['value_1'] = row['value'] if row['type'] == 1 else 0
            row['value_0'] = row['value'] if row['type'] == 0 else 0
        return row

df2 = df.groupby('id').apply(self.concat_rows).reset_index(drop=True)

But I get the following the following table with the modified numbers

    id  value    type  value_1  value_0
0  104   7999     0        0     7999
1  105     99     1       99      399
2  108  10770     0    12118    10770

Data:

{'id': [104, 105, 108, 108],
 'type': [0, 1, 0, 1],
 'value': [7999, 196193579, 245744, 93310128]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2025-01-22 06:29:17

看来您也想保留“类型”列值。因此,您可以使用 groupby + first 来获取“type”列;然后使用pivot获取剩余列并将其合并到“type”和“id”列:

out = (df.groupby('id')['type'].first().reset_index()
       .merge(df.pivot('id', 'type', 'value').add_prefix('value_')
              .fillna(0).reset_index(), on='id'))

pivot + assign

out = (df.pivot('id', 'type', 'value')
       .add_prefix('value_').fillna(0)
       .assign(type=df.groupby('id')['type'].first())
       .reset_index()
       [['id','type','value_0','value_1']]
       .rename_axis(columns=[None]))

输出:

    id  type   value_0      value_1
0  104     0    7999.0          0.0
1  105     1       0.0  196193579.0
2  108     0  245744.0   93310128.0

It seems you want to keep "type" column values as well. So you could use groupby + first to get the "type" column; then use pivot to get the remaining columns and merge it to the "type" and "id" columns:

out = (df.groupby('id')['type'].first().reset_index()
       .merge(df.pivot('id', 'type', 'value').add_prefix('value_')
              .fillna(0).reset_index(), on='id'))

or pivot + assign:

out = (df.pivot('id', 'type', 'value')
       .add_prefix('value_').fillna(0)
       .assign(type=df.groupby('id')['type'].first())
       .reset_index()
       [['id','type','value_0','value_1']]
       .rename_axis(columns=[None]))

Output:

    id  type   value_0      value_1
0  104     0    7999.0          0.0
1  105     1       0.0  196193579.0
2  108     0  245744.0   93310128.0
深海夜未眠 2025-01-22 06:29:17

您可以使用:

df = df.pivot_table(index=['id'], columns=['type'], values=['value'], fill_value=0).reset_index()
df.columns = ['_'.join(map(str, col)).strip('_') for col in df.columns]

输出

    id   value_0      value_1
0  104    7999.0          0.0
1  105       0.0  196193579.0
2  108  245744.0   93310128.0

You can use:

df = df.pivot_table(index=['id'], columns=['type'], values=['value'], fill_value=0).reset_index()
df.columns = ['_'.join(map(str, col)).strip('_') for col in df.columns]

OUTPUT

    id   value_0      value_1
0  104    7999.0          0.0
1  105       0.0  196193579.0
2  108  245744.0   93310128.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文