【发布时间】: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_mean 和 time_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”方式(熊猫)。
【问题讨论】: