Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove substring in array of strings column in dataframe

I have dataframe column which is array of strings

````
| fruits                         |
|--------------------------------|
|['fruit=apple', 'fruit=banana'] |
|['fruit=orange', 'fruit=banana']|
|['fruit=apple', 'fruit=orange'] |
|['fruit=orange', 'fruit=orange']|
````

I want to get result like

``
| fruits             |
|--------------------|
|['apple', 'banana'] |
|['orange', 'banana']|
|['apple', 'orange'] |
|['orange', 'orange']|
``

I want to remove substring
'fruit='

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can apply a function to the column. In this case split each string by = and take the last element of the result.

df['fruits'].apply(lambda x: [f.split('=')[-1] for f in x])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading