9870
9870

Reputation: 33

Pandas Grouper showing TypeError

I used Pandas Groupby to group data but got

TypeError: sequence item 0: expected str instance, float found

My code:

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

Then I used:

df = df.applymap(str)

But got

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

Upvotes: 1

Views: 319

Answers (1)

jezrael
jezrael

Reputation: 862511

Use:

#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()))

Upvotes: 1

Related Questions