熊猫石斑鱼显示TypeError

时间:2019-12-10 11:26:11

标签: python pandas

我使用Pandas Groupby对数据进行分组,但得到了

  

TypeError:序列项0:预期的str实例,找到浮点数

我的代码:

df.groupby(pd.Grouper(key='Date',freq='W-Sun')).apply(lambda x: " ".join(x['Text']))

然后我使用了:

df = df.applymap(str)

但是得到了

  

TypeError:仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Index”的实例

1 个答案:

答案 0 :(得分:1)

使用:

#if need convert values to strings
df.groupby(pd.Grouper(key='Date',freq='W-Sun'))['Text'].apply(lambda x: " ".join(x.astype(str)))

#if need remove NaNs and Nones values in Text column
df.groupby(pd.Grouper(key='Date',freq='W-Sun'))['Text'].apply(lambda x: " ".join(x.dropna()))

#if need remove NaNs and Nones values in Text column and cast to str
df.groupby(pd.Grouper(key='Date',freq='W-Sun'))['Text'].apply(lambda x: " ".join(x.astype(str).dropna()))