Reputation: 55
I would like to fill 2 destination pandas columns at once for performance reasons. A sample value from the source column is '42844 - News - BENL'. I need to extract '42844' and 'News'. Additional values can be ignored.
df["_site_id"], df["_site_name"], rest = df["website"].apply(lambda x: x.split(" - ", 2))
I tested this code and i get a 'ValueError: too many values to unpack (expected 3)'
Upvotes: 2
Views: 89
Reputation: 88236
For vectorized string operations in pandas you have str
. So instead you can use Series.str.split
. Also for this assignment to work you have to first "expand" the resulting list into n
new columns, so set expand=True
:
df["_site_id"], df["_site_name"], _ = df.website.str.split(' - ', 2, expand=True)
Which could also be done by indexing on a list of columns and splatting on the lhs as:
*df[['_site_id', '_site_name']], _ = df.website.str.split(' - ', 2, expand=True)
Note: For multiple assignment to work there must be 3
columns as a result from splitting website
, otherwise you'll get a ValueError
saying that there are not enough values to unpack.
Upvotes: 3
Reputation: 323306
Let us do str.split
df["website"].str.split(" - ", 2, expand=True)
#df=df.join(df["website"].str.split(" - ", 2, expand=True).rename(columns={0:'_site_id', 1:'_site_name'}))
Upvotes: 1