【问题标题】:How to combine 2 rows into 1 row in pandas based on a column (obj)如何根据一列(obj)在熊猫中将2行合并为1行
【发布时间】:2020-09-28 11:36:45
【问题描述】:

这是我的数据框信息:

df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
A_mean          6 non-null float64
time_range      6 non-null object
time_range_1    6 non-null object
B               6 non-null object
dtypes: float64(1), object(3)

这里是df

df

index    A_mean    time_range    time_range_1    B
0       5.936667       07       08:00 - 08:59   1001
1       5.103241       08       08:00 - 08:59   1001
2       5.267687       09       09:00 - 09:59   1001

我试图合并这两行:

index    A_mean    time_range    time_range_1    B
0       5.936667       07       08:00 - 08:59   1001
1       5.103241       08       08:00 - 08:59   1001

在一行中,所需的输出应如下所示:

index    A_mean    time_range    time_range_1    B
0       5.519954       08       08:00 - 08:59   1001

** P/S:最重要的列将是 A_meantime_range_1,B 列应该保持不变。

我试过.groupby,但我得到了错误:

df2 = df.groupby('time_range_1')['A_mean'].apply(' '.join).reset_index()

TypeError: sequence item 0: expected str instance, numpy.float64 found

任何解决方案都会受到赞赏,但以“pythonic”方式(熊猫)。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    你可以使用:

    df.groupby(['time_range_1', 'B'], as_index=False).agg({'A_mean': 'mean', 'time_range':'last'})
    

    结果:

        time_range_1     B    A_mean  time_range
    0  08:00 - 08:59  1001  5.519954           8
    1  09:00 - 09:59  1001  5.267687           9
    

    【讨论】:

    • 感谢@liliscent 的快速回复!哦,可以使用.agg!你拯救了我的一天! :)
    【解决方案2】:

    尝试另一种方法来解决,但不是在 1 个代码中:

    df2= df.groupby(df.time_range_1).mean().reset_index()
    df2['B'] = '1001'
    
    Output:
    index    time_range_1    A_mean      B
    0       08:00 - 08:59   5.519954    1001
    1       09:00 - 09:59   5.267687    1001
    

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 1970-01-01
      • 2019-07-02
      • 2021-03-06
      • 1970-01-01
      • 2019-07-20
      • 2020-11-11
      • 2022-08-18
      • 2017-05-28
      相关资源
      最近更新 更多