'All variables of particular column to a single list
I need help converting variables of one column into one list. When I tried myself I got output as a list of lists, but I need one list.
My column looks like this:
30% down payment
Limited Units , 3 Yrs Payment Plan , La Violeta TH
OPEN HOUSE, FEB 25-26 , Motivated Seller
Bright and Spacious Apt , Serene Location
Exclusive , Affordable Options Available
Here is my code:
title=dataset['title']
flat_title_list=[]
for lists in title:
flat_title_list.append(lists)
flat_title_list
Here is the output I got:
['30% down payment',
'Limited Units , 3 Yrs Payment Plan , La Violeta TH',
'OPEN HOUSE, FEB 25-26 , Motivated Seller',
'Bright and Spacious Apt , Serene Location',
'Exclusive , Affordable Options Available']
However, I want it to look like this:
['30% down payment',
'Limited Units', '3 Yrs Payment Plan' , 'La Violeta TH',
'OPEN HOUSE', 'FEB 25-26' , 'Motivated Seller',
'Bright and Spacious Apt' , 'Serene Location',
'Exclusive' , 'Affordable Options Available']
Thank you!
Solution 1:[1]
You might try to flatten the inner list of lists by nesting another for loop, e.g. something like that:
title=dataset['title']
flat_title_list=[]
for lists in title:
for inner_list in lists:
flat_title_list.append(inner_list)
Solution 2:[2]
If you need split on commas something like this should work:
[item for record in dataset['title'] for item in record.split(' , ')]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | xWulf |
| Solution 2 | Aivar Paalberg |
