Reputation: 9
I have data frame same like below:
Name Rating Review Price
1 The park NaN NaN 5040
2 The Westin Good 7.6 NaN 6045
3 Courtyard NaN NaN 4850
4 Radisson Excellent 9.8 NaN 7050
5 Banjara Average 6.7 NaN 5820
6 Mindspace NaN NaN 8000
My required output is like this:
Name Review Rating Price
1 The park NaN NaN 5040
2 The Westin Good 7.6 6045
3 Courtyard NaN NaN 4850
4 Radisson Excellent 9.8 7050
5 Banjara Average 6.7 5820
6 Mindspace NaN NaN 8000
I use this split function:
df[["review","ratings"]] = df["rating"].str.split(expand=True)
But I got 'Columns must be same length as key' this type error.
How to split this type of data can anyone help me?
Upvotes: 0
Views: 45
Reputation: 863256
Problem is there is multiple space, not only one at least in one splitted value.
You can add n=1
for split after first space:
df[["review","ratings"]] = df["Rating"].str.split(expand=True, n=1)
Or use rsplit
with n=1
for split by last space:
df[["review","ratings"]] = df["Rating"].str.rsplit(expand=True, n=1)
Another idea is use Series.str.extract
with regex for get all values before space before float:
df[["review","ratings"]] = df["Rating"].str.extract('(.*)\s+(\d+\.\d+)')
Upvotes: 1