我得到了以下数据帧:
import pandas as pd
test = pd.DataFrame([["Bugatti","Veyron"],
["Bugatti","Chiron"],
["VW","Golf"],
["VW","Passat"],
["VW","Polo"],
["VW","Caddy"],
["Porsche","Taycan"],
["Porsche","911"]],
columns=['Brand', 'Model'])
test我想更改它,以便您将品牌(Bugatti,VW,Porsche)作为列标题,并在其下方的列中包含车型。当然,这只是一个更大的列表的示例。有没有一种快速重排数据帧的方法?之后它应该看起来像这样。
Bugatti VW Porsche
Veyron Golf Taycan
Chiron Passat 911
Polo
Caddy 发布于 2019-11-28 17:48:09
您可以尝试转置或枢轴。转置将为您提供同一品牌的多个列。而pivot在结果中将具有空值。
转置:
test.T结果
Brand Bugatti Bugatti VW VW VW VW Porsche Porsche
Model Veyron Chiron Golf Passat Polo Caddy Taycan 911透视: test.pivot(columns='Brand',values='Model')
结果
Bugatti Porsche VW
Veyron NaN NaN
Chiron NaN NaN
NaN NaN Golf
NaN NaN Passat
NaN NaN Polo
NaN NaN Caddy
NaN Taycan NaN
NaN 911 NaNhttps://stackoverflow.com/questions/59085359
复制相似问题