I have a .csv file with rows like: apple,banana,orange,carambola and red,blue,purple,black

And I am trying to split like: apple,banana apple,orange apple,carambola and red,blue red,purple red,black

How do I do that?

Update AttributeError: 'Series' object has no attribute 'split'

Update 2 AttributeError: 'DataFrame' object has no attribute 'split'

2 Answers

0
Victor Oliveira On

You can iterate for each row of your csv file and do something like that:

csv_row = csv_row.split(',')
new_list = []
for i in range(1,len(csv_row)):
    new_list.append(','.join((csv_row[0],csv_row[i])))

1
shotgunner On

It seems you want to add the first element of your row to all of others in the row. You can do like this:

row = 'apple,banana,orange,carambola'
splitted_row = row.split(",")
print ([",".join([splitted_row[0], i]) for i in splitted_row[1:]])
# ['apple,banana', 'apple,orange', 'apple,carambola']