从variable1的值列表中为variable1的每个值创建新的数据帧?

时间:2017-04-30 09:46:52

标签: python list pandas dataframe melt

我有一些来自不同州的邮政编码列表,例如

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

我想像这样创建一个pandas数据框(顺序并不重要):

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

怎么做?

1 个答案:

答案 0 :(得分:7)

您可以先以宽格式构建DataFrame,然后使用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

对于不同的长度:

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