我是一位有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:
我有一个包含ID、名称、日期时间和值的数据框。当我使用此数据框的dtypes时,结果如下:
ID - int64
time - object
value - int
我希望能够按照ID对数据框进行分组,并按时间顺序对行进行排序。我尝试了以下方法:
df['time'] = pd.to_datetime(df['time'])
p= df.groupby(['ID'])
我尝试了一下,
p.sort_values(['time'])
但是遇到了一个错误:
AttributeError: Cannot access callable attribute 'sort_values' of 'DataFrameGroupBy' objects, try using the 'apply' method
然后我尝试了一下,
p['time'].apply(lambda x: x.sort_values())
这似乎只对该列有效。我无法对所有列进行排序。我想根据时间变量分组ID并对整个数据框进行排序。我知道这可能很容易。但是尝试了很长时间都没有结果。有人能帮我做到这一点吗?
print df.set_index('value').groupby(['ID']).apply(lambda x: x['time'].sort_values()).reset_index()
。 - jezrael