I have a dataframe with a single column that contains States followed by their particular regions. The states are already in alphabetical order. I want to convert this single column into 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
The above code returns:
State Region
0 Alabama Alabama
1 Alabama Alabama
... ... ...
500 Alabama Alabama
Note: In my example I only use Alabama and Alaska but there are another 500 rows below this for the other states. Any idea how I can get to the following:
State Region
0 Alabama Auburn
1 Alabama Florence
2 Alabama Troy
3 Alaska Fairbanks
... ... ...