从一个列系列创建两列数据框

问题描述 投票:1回答:2

我有一个包含州和城市数据的系列。首先是一个州,在它的所有城市之下。然后是另一个州及其城市,依此类推。如何将该系列转换为[[州],[城市]]这样的数据框。这是我的代码:

U_towns = pd.read_table('university_towns.txt', header = None).rename(columns = {0 :'Borrador'})
U_towns = U_towns['Borrador'].str.split('(', n=2, expand = True).rename(columns = {0 :'Borrador'})
U_towns['State?'] = U_towns['Borrador'].apply(lambda x: 'State' if 'edit' in x else '')
U_towns = U_towns[['Borrador','State?']]

This is the result of my code

不知道如何将州和城市移动到不同的列,而每个城市在其旁边的列中都有其州

python pandas
2个回答
1
投票

让我们做

df['State']=df.loc[df['State?']=='State','State?']
df.State=df.State.ffill()
df=df[df['State?']!='State']

0
投票

我认为在这种情况下,最好使用由Series组成的Dictionary。例如:

#Series with cities 
data_1 = np.array(['city_1','city_2','city_3','city_4'])
state_1 = pd.Series(data_1)

data_2 = np.array(['city_5','city_6','city_7','city_8'])
state_2 = pd.Series(data_2)

#Making a dictionary of states
dict = {
      "State_1": state_1,
      "State_2": state_2,
     }
© www.soinside.com 2019 - 2024. All rights reserved.