从 variable2 的值列表中为 variable1 的每个值创建新的数据框?
Create new dataframe from list of values of variable2 for each value of variable1?
我有一些不同州的邮政编码列表,格式如下
stateA_postcode = [12345, 23456, 34567, ...]
stateB_postcode = [11111, 22222, 33333, ...]
我想像这样创建一个 pandas 数据框(顺序无关紧要):
postcode state
0 11111 B
1 12345 A
... ...
如何操作?
可以先构造宽形式的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
我有一些不同州的邮政编码列表,格式如下
stateA_postcode = [12345, 23456, 34567, ...]
stateB_postcode = [11111, 22222, 33333, ...]
我想像这样创建一个 pandas 数据框(顺序无关紧要):
postcode state
0 11111 B
1 12345 A
... ...
如何操作?
可以先构造宽形式的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