[英]Creating two column dataframe from one column series
I have a Series with data of States and Cities.我有一个包含州和城市数据的系列。 First comes a State and underneath It all of its cities.
首先是一个州,在它之下是所有的城市。 Then another state with its cities under it and so on.
然后是另一个州及其下属的城市,依此类推。 How can I transform that series into a Dataframe like ['State', 'City'].
如何将该系列转换为 ['State', 'City'] 之类的数据框。 This is my code:
这是我的代码:
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?']]
Dont know how to move States and cities to diferent columns in a way where each city has its state in a column next to it不知道如何将州和城市移动到不同的列中,每个城市在旁边的列中都有自己的状态
Let us do让我们做
df['State']=df.loc[df['State?']=='State','State?']
df.State=df.State.ffill()
df=df[df['State?']!='State']
I think it would be better to use a Dictionary consisting of Series in this case.我认为在这种情况下最好使用由 Series 组成的 Dictionary 。 For example :
例如 :
#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,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.