转置数据框列值作为python中的列标题

时间:2019-05-23 11:14:03

标签: python python-3.x pandas dataframe

一个熊猫数据框具有三列(HHID,名称,模型)。我想将模型列值作为列标题。如果存在,“模型”中的值应为“是”是与HHID列相对应的模型值。

有人可以为此提供python代码吗?

附加输入的屏幕截图。

Input screenshot

输出应为:

Output Screenshot

1 个答案:

答案 0 :(得分:0)

您可以尝试pd.get_dummies

     hhid _NAME_    model
0   10006  flag4      NaN
1   10014  flag4      NaN
2   10047  flag4  make323
3   10055  flag4      NaN
4   10066  flag4  make268
5   10074  flag4      NaN
6   10085  flag4      NaN
7   10117  flag4  make146
8   10126  flag4      NaN
9   10136  flag4      NaN
10  10147  flag4  make148
>>> pd.get_dummies(df, columns=['model'])

     hhid _NAME_  model_make146  model_make148  model_make268  model_make323
0   10006  flag4              0              0              0              0
1   10014  flag4              0              0              0              0
2   10047  flag4              0              0              0              1
3   10055  flag4              0              0              0              0
4   10066  flag4              0              0              1              0
5   10074  flag4              0              0              0              0
6   10085  flag4              0              0              0              0
7   10117  flag4              1              0              0              0
8   10126  flag4              0              0              0              0
9   10136  flag4              0              0              0              0
10  10147  flag4              0              1              0              0
>>> # by setting prefix and prefix_sep to empty string
>>> # you could make your values as column names
>>> pd.get_dummies(df, columns=['model'], prefix='', prefix_sep='')

     hhid _NAME_  make146  make148  make268  make323
0   10006  flag4        0        0        0        0
1   10014  flag4        0        0        0        0
2   10047  flag4        0        0        0        1
3   10055  flag4        0        0        0        0
4   10066  flag4        0        0        1        0
5   10074  flag4        0        0        0        0
6   10085  flag4        0        0        0        0
7   10117  flag4        1        0        0        0
8   10126  flag4        0        0        0        0
9   10136  flag4        0        0        0        0
10  10147  flag4        0        1        0        0