0

I've got some lists of postcodes from different states, in the form such as

stateA_postcode = [12345, 23456, 34567, ...]
stateB_postcode = [11111, 22222, 33333, ...]

And I would like to create a pandas dataframe like this (order doesn't matter):

     postcode    state
0    11111       B
1    12345       A
...         ...

How to do this?

smci
  • 32,567
  • 20
  • 113
  • 146
Kevin Fang
  • 1,966
  • 2
  • 16
  • 31

1 Answers1

7

You can first construct the DataFrame in the wide form and then use melt:

df = pd.DataFrame({'A': stateA_postcode, 'B': stateB_postcode})

pd.melt(df, var_name='state', value_name='postcode')
Out: 
  state  postcode
0     A     12345
1     A     23456
2     A     34567
3     B     11111
4     B     22222
5     B     33333

For different lengths:

stateA_postcode = [12345, 23456, 34567]
stateB_postcode = [11111, 22222]


df = pd.DataFrame({'postcode': stateA_postcode + stateB_postcode,
                   'state': ['A']*len(stateA_postcode) + 
                            ['B']*len(stateB_postcode)})

df
Out: 
   postcode state
0     12345     A
1     23456     A
2     34567     A
3     11111     B
4     22222     B
ayhan
  • 70,170
  • 20
  • 182
  • 203
  • Unfortunately these postcodes lists are not in the same length. However I think I can create and melt all the states separately and use pd.concat. Thanks for the answer. – Kevin Fang Apr 30 '17 at 10:05
  • @Kevin.Fang Yes the equal length assumption was unrealistic, sorry about that. I've posted an alternative. – ayhan Apr 30 '17 at 10:09
  • That's it! Thank you very much! – Kevin Fang Apr 30 '17 at 11:24