使用对象和datetime64[ns]进行分组导致空的数据框。

huangapple go评论105阅读模式
英文:

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

我想使用以下命令进行groupbysum操作:

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 groupbyand 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'])

huangapple
  • 本文由 发表于 2023年8月9日 17:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定