David542
David542

Reputation: 110382

Create a new column which is cast to a string in pandas

What would be the proper way to assign a stringified column to a dataframe, as I would like to keep the original so I don't want to use .astype({'deliveries': 'str'). SO far I have:

df = ( df.groupby('path')
      .agg(agg_dict)
      .assign(deliveries_str=df['deliveries'].str ??)
)

What would be the proper way to do this?


I also tried the following but I get an unhashable type error:

.assign(deliveries_str=lambda x: x.deliveries.str)

TypeError: unhashable type: 'list'

Upvotes: 0

Views: 37

Answers (1)

BENY
BENY

Reputation: 323316

You need try change .str since it is a function

.assign(deliveries_str=lambda x: x.deliveries.astype(str))

Adding mask

.assign(deliveries_str=lambda x: x['deliveries'].astype(str).mask(x['deliveries'].isnull()))

Upvotes: 1

Related Questions