我使用 Pandas Groupby 对数据进行分组,但得到了
TypeError: sequence item 0: expected str instance, float found
我的代码:
df.groupby(pd.Grouper(key='Date',freq='W-Sun')).apply(lambda x: " ".join(x['Text']))
然后我用了:
df = df.applymap(str)
但是得到了
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
最佳答案
用途:
#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()))
关于python - Pandas 石斑鱼显示类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59266155/