我正在对一个DataFrame执行分组操作。在每个组上,我需要重命名两列并删除一列,以便每个组具有以下形式:
index(timestamp) | column-x | column-y
... | .... | .....
索引是一个时间戳,每个组都将共享此时间戳。相反,'column-x'和'column-y'对于每个组将是不同的。因此,我的目标是在索引上连接所有组,以便我拥有一个唯一的DataFrame,如下所示:
index(timestamp) | column-x1 | column-y1 | column-x2 | column-y2 | ...
... | ..... | ...... | ....... | ....... | ...
我应用于每个组的功能是(在迭代时可以进行原地编辑吗?):
def process_ssp(df_ssp):
sensor_name = df_ssp.iloc[0]['subsystem-sensor-parameter'] # to be used as column name
df_ssp.rename(columns = {
'value_raw': '%s_raw' % sensor_name,
'value_hrf': '%s_hrf' % sensor_name,
}, inplace = True)
df_ssp.drop('subsystem-sensor-parameter', axis='columns', inplace=True) # since this is the column I am grouping on I guess this isn't the right thing to do?
return df_ssp
然后我调用:
res = df_node.groupby('subsystem-sensor-parameter', as_index=False).apply(process_ssp)
这会产生错误:
ValueError: cannot reindex from a duplicate axis
编辑: 数据集示例https://drive.google.com/file/d/1RvPE1t3BmjeaqCNkVqGwmokCFQQp77n8/view?usp=sharing