简体   繁体   中英

How to replace a string that is a part of a dataframe with a list in pandas?

I am a beginner at coding, and since this is a very simple question, I know there must be answers out there. However, I've searched for about a half hour, typing countless queries in google, and all has flown over my head.

Lets say I have a dataframe with columns "Name", "Hobbies" and 2 people, so 2 rows. Currently, I have the hobbies as strings in the form "hobby1, hobby2". I would like to change this into ["hobby1", "hobby2"]

hobbies_as_string = df.iloc[0, 2]
hobbies_as_list = hobbies_as_string.split(',')
df.iloc[0, -2] = hobbies_as_list

However, this falls to an error, ValueError: Must have equal len keys and value when setting with an iterable. I don't understand why if I get hobbies_as_string as a copy, I'm able to assign the hobbies column as a list no problem. I'm also able to assign df.iloc[0,-2] as a string, such as "Hey", and that works fine. I'm guess it has to do the with ValueError. Why won't pandas let me assign it as a list??

Thank you very much for your help and explanation.

Are you looking to apply a split row-wise to each value into a list?

import pandas as pd
df = pd.DataFrame({'Name' : ['John', 'Kate'],
              'Hobbies' : ["Hobby1, Hobby2", "Hobby2, Hobby3"]})
df['Hobbies'] = df['Hobbies'].apply(lambda x: x.split(','))
df

OR if you are not a big lambda exer, then you can do str.split() on the entire column, which is easier:

import pandas as pd
df = pd.DataFrame({'Name' : ['John', 'Kate'],
              'Hobbies' : ["Hobby1, Hobby2", "Hobby2, Hobby3"]})
df['Hobbies'] = df['Hobbies'].str.split(",")
df

Output:

    Name    Hobbies
0   John    [Hobby1, Hobby2]
1   Kate    [Hobby2, Hobby3]

Another way of doing it

df=pd.DataFrame({'hobbiesStrings':['"hobby1, hobby2"']})
df

replace ,whitespace with "," and put hobbiesStrings values in a list

x=df.hobbiesStrings.str.replace('((?<=)(\,\s+)+)','","').values.tolist()

x

Here I use regex expressions Basically I am replacing comma \, followed by whitespace \s with ","

rewrite column s using df.assign

df=df.assign(hobbies_stringsnes=[x])

Chained together

 df=df.assign(hobbies_stringsnes=[df.hobbiesStrings.str.replace('((\,\s))','","').values.tolist()])
df

Output

在此处输入图像描述

Use the "at" method to replace a value with a list

import pandas as pd
# create a dataframe
df = pd.DataFrame(data={'Name': ['Stinky', 'Lou'], 
                        'Hobbies': ['Shooting Sports', 'Poker']})
# replace Lous hobby of poker with a list of degen hobbies with the at method
df.at[1, 'Hobbies'] = ['Poker', 'Ponies', 'Dice']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM