简体   繁体   中英

split a dataframe column into equal parts

I want to break this column into three components: city, state, zip. I was considering something like this: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html . However, I think the column needs to be a series first? Any help would be great.

import pandas as pd
suite_list
hospital_list =pd.DataFrame(suite_list, columns=['name', 'address_1', 'address_2', 'city_state_zip', 'phone'])

city_state_zip example = Sahuarita, AZ, 85629. I want 'Sahuarite', 'AZ,' '85629' to show up as individual columns in the DataFrame.

You can try something like this,

df = pd.DataFrame({'Name' : ('john','doe','smith'), 'address_1' : (105,305,505), 'address_2' : ('path','lane','route'),\
           'city_state_zip': ('Sahuarita, AZ, 85629', 'Sahuarita1, AZ1, 75629', 'Sahuarita2, AZ2, 65629')})

df

        Name    address_1   address_2   city_state_zip
0        john   105             path    Sahuarita, AZ, 85629
1        doe    305             lane    Sahuarita1, AZ1, 75629
2        smith  505             route   Sahuarita2, AZ2, 65629

and then try,

df["col1"],df["col2"],df["col3"] = zip(*df["city_state_zip"].str.split().tolist())

the output df will be

       Name address_1   address_2   city_state_zip      col1     col2     col3
    0   john    105     path    Sahuarita, AZ,85629     Sahuarita   AZ     85629
    1   doe 3   05      lane    Sahuarita1, AZ1,5629    Sahuarita1  AZ1    75629
    2   smith   505     route   Sahuarita2, AZ2,65629   Sahuarita2  AZ2    65629

This should give what are you looking for.

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