![](/img/trans.png)
[英]Pandas DataFrame: How to convert binary columns into one categorical column?
[英]How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?
我有一個只有一欄的數據框,其中包含各州及其后面的特定區域。 狀態已經按字母順序排列。 我想將此單列轉換為兩列。
import pandas as pd
places = [{'State':'Alabama'},{'State':'Auburn'},{'State':'Florence'},{'State':'Troy'},{'State':'Alaska'},{'State':'Fairbanks}]
df0 = pd.DataFrame(places)
s1 = df0.loc[df['State']=='Alabama'].index[0] #return row of state 'Alabama'
s2 = df0.loc[df['State']=='Alaska'].index[0] #return row of state 'Alaska'
df1 = df0 # create new dataframe
for index, row in df0.iterrows(): # parse single column into two columns data
if index == s1:
df1['State']='Alabama'
elif index <= s2:
df1['Region']=row['State']
df
上面的代碼返回:
State Region
0 Alabama Alabama
1 Alabama Alabama
... ... ...
500 Alabama Alabama
注意:在我的示例中,我僅使用阿拉巴馬州和阿拉斯加,但其他州在此下方還有500行。 任何想法我如何可以做到以下幾點:
State Region
0 Alabama Auburn
1 Alabama Florence
2 Alabama Troy
3 Alaska Fairbanks
... ... ...
首先,您需要擁有狀態的所有數據:下載並調用狀態會很好。 我將嘗試在前幾行中手動調用它
#This is not necessary. Download the file and call states directly from it.
import re
import pandas as pd
a = 'http://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/download/1/'
states = eval(re.sub("(?<=\w')\s+(?=[^:])",",","".join(['{']+list(pd.read_csv(a).index))))
只需按原樣運行此代碼,然后執行print(states)
查看IDE中是否具有所有狀態。也可以從此處直接復制粘貼為states
現在我們有了所有狀態,就可以解決問題
import numpy as np
places = [{'State':'Alabama'},{'State':'Auburn'},{'State':'Florence'},{'State':'Troy'},{'State':'Alaska'},{'State':'Fairbanks'}]
df0 = pd.DataFrame(places)
df0['Regions'] = df0.State
B = pd.DataFrame(df0.set_index('Regions').State.replace(regex=f'^(?!({"|".join(states.values())})).*',value=np.nan).reset_index()).ffill()
B[B.State!=B.Regions].iloc[:,[1,0]]
State Regions
1 Alabama Auburn
2 Alabama Florence
3 Alabama Troy
5 Alaska Fairbanks
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.