Home > database >  change value in pandas dataframe using iteration
change value in pandas dataframe using iteration

Time:02-20

I have a training data set of the following format:

print(data.head(5))
#Output



           0  1
0  a b c d e  1
1  a b c d e  1
2  a b c d e  1
3  a b c d e  1
4  a b c d e  1

It is a text classification task and I am trying to split the text "a b c d e" in to a Python list. I tried iteration:

data #the dataset
len_data = len(data)
for row_num in range(len_data):
    data.loc[row_num, 0] = data.loc[row_num, 0].split(" ")

However this doesn't work and returned the error Must have equal len keys and value when setting with an iterable. Could someone help me with this problem? Many thanks!

CodePudding user response:

Use str.split:

df[0] = df[0].str.split()
print(df)

# Output
                 0  1
0  [a, b, c, d, e]  1
1  [a, b, c, d, e]  1
2  [a, b, c, d, e]  1
3  [a, b, c, d, e]  1
4  [a, b, c, d, e]  1

Setup:

data = {0: {0: 'a b c d e', 1: 'a b c d e'}, 1: {0: 1, 1: 1}}
df = pd.DataFrame(data)
  • Related