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

Extract a list of values from a column in a pandas dataframe

I’m trying to extract a list of values from a column in a dataframe.

For example:

# dataframe with "num_fruit" column 
fruit_df = pd.DataFrame({"num_fruit": ['1 "Apple"', 
                                        '100 "Peach Juice3" 1234 "Not_fruit" 23 "Straw-berry" 2 "Orange"']})
# desired output: a list of values from the "num_fruit" column 
[['1 "Apple"'],
 ['100 "Peach Juice3"', '1234 "Not_fruit"', '23 "Straw-berry"', '2 "Orange"']]

Any suggestions? Thanks a lot.

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

What I’ve tried:

import re 

def split_fruit_val(val):
    return re.findall('(\d+ ".+")', val)

result_list = []
for val in fruit_df['num_fruit']:
    result = split_fruit_val(val)
    result_list.append(result)

print(result_list) 
#output: some values were not split appropriately 
[['1 "Apple"'],
 ['100 "Peach Juice3" 1234 "Not_fruit" 23 "Straw-berry" 2 "Orange"']]

>Solution :

Lets split with positive lookahead for a number

fruit_df['num_fruit'].str.split(r'\s(?=\d+)')

0                                          [1 "Apple"]
1    [100 "Peach Juice3", 1234 "Not_fruit", 23 "Str...
Name: num_fruit, dtype: object
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