英文:
Groupby with object and datetime64[ns] leas to empty dataframe
问题
我有一个包含以下列的数据框:
print(df.dtypes)
Daten float64
timepoint datetime64[ns]
Level object
Sublevel object
dtype: object
以及以下条目:
Daten timepoint Level Sublevel
1 2019-01-01T00:00:00.000 A AA
1 2019-01-01T00:00:00.000 A AA
2 2019-01-01T00:00:00.000 A AB
2 2019-01-01T00:00:00.000 B BA
1 2019-02-01T00:00:00.000 A AA
我想使用以下命令进行groupby
和sum
操作:
df= df.groupby(
['Level', 'timepoint']).agg({'Daten': 'sum'}).reset_index()
为什么会导致一个空的数据框?
我期望的输出应该是:
timepoint Level Daten
2019-01-01T00:00:00.000 A 4
2019-01-01T00:00:00.000 B 2
2019-02-01T00:00:00.000 A 1
英文:
I have a dataframe with the following columns:
print(df.dtypes)
Daten float64
timepoint datetime64[ns]
Level object
Sublevel object
dtype: object
and the following entries:
Daten timepoint Level Sublevel
1 2019-01-01T00:00:00.000 A AA
1 2019-01-01T00:00:00.000 A AA
2 2019-01-01T00:00:00.000 A AB
2 2019-01-01T00:00:00.000 B BA
1 2019-02-01T00:00:00.000 A AA
I want to groupby
and sum
with the following command:
df= df.groupby(
['Level', 'timepoint']).agg({'Daten': 'sum'}).reset_index()
why does this lead to an empty dataframe?
My expected output would be:
timepoint Level Daten
2019-01-01T00:00:00.000 A 4
2019-01-01T00:00:00.000 B 2
2019-02-01T00:00:00.000 A 1
答案1
得分: 0
不知何故,这段代码可以工作:
df['timepoint'] = df['timepoint'].astype(str)
df = df.groupby(['Level', 'timepoint']).agg({'Daten': 'sum'}).reset_index()
df['timepoint'] = pd.to_datetime(df['timepoint'])
英文:
Somehow this works:
df['timepoint'] = df['timepoint'].astype(str)
df = df.groupby(
['Level', 'timepoint']).agg({'Daten': 'sum'}).reset_index()
df['timepoint'] = pd.to_datetime(df['timepoint'])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论