Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Split Function in Reverse

I have a Pandas Dataframe with a column that looks like this:

    Car_Make
0   2017 Abarth 124 Spider ManualConvertible
1   2017 Abarth 124 Spider AutoConvertible
2   2017 Abarth 124 Spider ManualConvertible
3   2017 Abarth 124 Spider AutoConvertible
4   2017 Abarth 595 ManualHatch
5   2017 Abarth 595 AutoHatch

Three Questions:

1 How to save split data in panda in reverse order? - This solves my problem but I don't know how or why it works - can someone please explain this to me? I hate copy pasting without understanding why it works

df['Car_Make'].apply(lambda x:pd.Series(x.split()[::-1])) 

2 I have tried to replicate it using a user-defined function (that I can use again) with the following, but it doesn't appear to work (any help understanding why and the correct way to turn the Lambda function into a user-defined function

def f(x):
    df[x] = pd.Series(x.split()[::-1])
    return df

3 Is there a better way to split this column by space in reverse?

I have tried using Regex which works, but not on all rows as you can see row 4 and 5 a slightly different to the above.

Any help would be greatly appreciated.

Thanks, Adrian

like image 643
AdrianC Avatar asked Aug 07 '26 11:08

AdrianC


2 Answers

Here's a shot at your three questions:

1) Why does df['Car_Make'].apply(lambda x:pd.Series(x.split()[::-1])) work?

Break it down:

  1. df['Car_Make'] - the column with the data you want to operate on
  2. .apply() - a pandas DataFrame and Series method that will apply a function to either every column, or every row, in a DataFrame, or to every row in a Series.
  3. lambda x: - the function that will be applied by the .apply() method to every row of the Series. x represents the record object, which in your case is the string containing the Car_Make entries.
  4. pd.Series() - this will convert the value inside it into a pandas Series.
  5. x.split() - As mentioned in point 3, x is your string object, and split() is a string method that, when passed with no parameters, defaults to splitting a string by its spaces and returning each split object into a list.
  6. [::-1] - A handy list iterator that reverses a list, such as that returned by x.split(). The syntax for list iteration is [start_index:end_index:step]. Using a -1 step iterates through the list backwards.

Put that all together, and that code is iterating through every record in df['Car_Make'], splitting them, reversing the order of the split items, and returning the reversed list as a pandas Series object.

2) Replicating that with a defined function.

You are really close, only that the function needs to take a row/record as its argument, and needs to be called in the .apply() method. What you want to do is replace the lambda x, not the way it is applied.

Using what you have so far:

def f(x):
    return pd.Series(x.split()[::-1])

df['Car_Make'].apply(f)

3) Is there a better way?

If you want to split a string and then reverse the order of the items, no, this is a great way. If you only want to split a certain part of a string starting from the right, then rsplit() is a good method.

like image 194
jack6e Avatar answered Aug 09 '26 02:08

jack6e


Is this what you are looking for:

df.car_make.str.rsplit(' ', 1, expand=True)
# returns:
                        0                  1
0  2017 Abarth 124 Spider  ManualConvertible
1  2017 Abarth 124 Spider    AutoConvertible
2  2017 Abarth 124 Spider  ManualConvertible
3  2017 Abarth 124 Spider    AutoConvertible
4         2017 Abarth 595        ManualHatch
5         2017 Abarth 595          AutoHatch
like image 41
James Avatar answered Aug 09 '26 01:08

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!