简体   繁体   中英

Get help in constructing a pandas dataframe in Python

I am new to Python. I have a list of items, sth like this:

places = ['Alabama', 'Auburn', 'Jacksonville', 'Arizona', 'Flagstaff', 'Phoenix']

Please note that Auburn and Jacksonville are cities in Alabama state, whereas Flagstaff and Phoenix are cities in Arizona.

I need to create a pandas data frame with two columns ['State','City'] like the following:

    State |    City
  --------|----------
 Alabama  | Auburn
 Alabama  | Jacksonville
 Arizona  | Flagstaff
 Arizona  | Phoenix

I was trying to create a dictionary, which has 'state' as the keys and the 'cities' as the values for the corresponding keys. But it is not working for me, since my knowledge is limited in Pythoin right now.

Help is greatly appreciated.

Thanks

The keys in a dictionary have to be unique, so you would be overwriting some of the values if you did something where one state has multiple cities;

In [1]: {'Arizona': 'Flagstaff', 'Arizona': 'Phoenix'}
Out[1]: {'Arizona': 'Phoenix'}

On the other hand, if you know that your keys are unique, pandas.DataFrame.from_dict does the job.

In your case, you could pass the information as a list of pairs.

In [20]: df = pd.DataFrame([['Arizona', 'Flagstaff'], ['Arizona', 'Phoenix'], ['Alabama', 'Auburn']], columns=['State', 'City'])

In [22]: df
Out[22]: 
     State       City
0  Arizona  Flagstaff
1  Arizona    Phoenix
2  Alabama     Auburn

If you want to get rid of the redundant information that you are passing by including the state more than once, you could do something like

In [33]: cities = {'Alabama': ['Auburn', 'Jacksonville'], 'Arizona': ['Flagstaff', 'Phoenix']}

In [34]: pd.DataFrame(((k, c) for (k, v) in cities.items() for c in v), columns=['State', 'City'])
Out[34]: 
     State          City
0  Arizona     Flagstaff
1  Arizona       Phoenix
2  Alabama        Auburn
3  Alabama  Jacksonville

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