这是我的数据资料:
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任何解决办法都将受到赞赏,但以“琵琶”的方式(熊猫)。
发布于 2018-05-14 07:24:22
已经尝试了另一种方法来解决问题,但不是在一个代码中:
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 1001https://stackoverflow.com/questions/50324728
复制相似问题