Reputation: 1114
After some research on csv / pandas / etc to manipulate a huge csv file I decide to use pandas to slice just the information I need. Now I am able to get just what I need using a filter i.e. "Name"="Greg" where I just see rows when the column Name has Greg. However I would like now to create a pyhton List with all information of a specific column (i.e. City). How could I do that? Then I will work just with the List to sort, count, etc.
What I have:
import pandas as pd
all_data = pd.read_csv(
'myfile.csv', # file name
sep=',', # column separator
quotechar='"', # quoting character
encoding='utf-16',
na_values=0, # fill missing values with 0
usecols=[0,1,3], # columns to use
decimal='.') # symbol for decimals
slice1 = all_data[all_data['Name'] == 'Greg']
print (slice1)
Example of print (slice1):
Upvotes: 1
Views: 3770
Reputation: 863281
#output is Series - column City
slice1 = all_data.ix[all_data['Name'] == 'Greg', 'City']
#generate list from Series
L = all_data.ix[all_data['Name'] == 'Greg', 'City'].tolist()
Sample:
import pandas as pd
all_data = pd.DataFrame({'Name':['Greg','Greg','Greg','Adam'],
'Coutry':['US','UK','UK','UK'],
'City':['LA','LD','RE','LB']},
index=[221,564,800,500])
print (all_data)
City Coutry Name
221 LA US Greg
564 LD UK Greg
800 RE UK Greg
500 LB UK Adam
slice1 = all_data.ix[all_data['Name'] == 'Greg', 'City']
print (slice1)
221 LA
564 LD
800 RE
Name: City, dtype: object
L = all_data.ix[all_data['Name'] == 'Greg', 'City'].tolist()
print (L)
['LA', 'LD', 'RE']
Upvotes: 1