如何将 pandas 中的两行合并为一行?
我有这个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您也想保留“类型”列值。因此,您可以使用
groupby
+first
来获取“type”列;然后使用pivot
获取剩余列并将其合并
到“type”和“id”列:或
pivot
+assign
:输出:
It seems you want to keep "type" column values as well. So you could use
groupby
+first
to get the "type" column; then usepivot
to get the remaining columns andmerge
it to the "type" and "id" columns:or
pivot
+assign
:Output:
您可以使用:
输出
You can use:
OUTPUT