我有df:
ID url visits count_sec buys
0012ea90a6deb4eeb2924fb13e844136 aliexpress.com 3438 33067 25
0012ea90a6deb4eeb2924fb13e844136 ebay.com 9 44
001342afb153e2775649dc5ae0460605 ozon.ru 1 6
0019b08bc9bb8da21f3b8ecc945a67d3 aliexpress.com 24 2196
0019b08bc9bb8da21f3b8ecc945a67d3 bonprix.ru 42 1378
我需要转发它并获得
ID url
visits count_sec buys
aliexpress.com ebay.com ozon.ru aliexpress.com bonprix.ru
0012ea90a6deb4eeb2924fb13e844136 3438 33067 25 9 44 0 0 0 0 0 0 0 0 0 0
001342afb153e2775649dc5ae0460605 0 0 0 0 0 0 1 6 0 0 0 0 0 0 0
0019b08bc9bb8da21f3b8ecc945a67d3 0 0 0 0 0 0 0 0 0 24 2196 0 42 1378 0
我该怎么做?
答案 0 :(得分:3)
您可以使用pivot_table
,但在url
列中缺少热门MultiIndex
。值也只是随机的,因为不清楚每列包含哪些值:
#new column with value `url`
df['url1'] = 'url'
df1 = df.pivot_table(index='ID', values=['visits','count_sec','buys'],columns=['url1','url'])
#swap first and second level in MultiIndex in columns
df1.columns = df1.columns.swaplevel(0,1)
#remove columns names
df1 = df1.rename_axis((None,None,None), axis=1)
print (df1)
url \
visits
aliexpress.com bonprix.ru ebay.com ozon.ru
ID
0012ea90a6deb4eeb2924fb13e844136 3438.0 NaN NaN NaN
001342afb153e2775649dc5ae0460605 NaN NaN NaN NaN
0019b08bc9bb8da21f3b8ecc945a67d3 NaN NaN NaN NaN
\
count_sec
aliexpress.com bonprix.ru ebay.com ozon.ru
ID
0012ea90a6deb4eeb2924fb13e844136 33067.0 NaN 9.0 NaN
001342afb153e2775649dc5ae0460605 NaN NaN NaN 1.0
0019b08bc9bb8da21f3b8ecc945a67d3 24.0 42.0 NaN NaN
buys
aliexpress.com bonprix.ru ebay.com ozon.ru
ID
0012ea90a6deb4eeb2924fb13e844136 25.0 NaN 44.0 NaN
001342afb153e2775649dc5ae0460605 NaN NaN NaN 6.0
0019b08bc9bb8da21f3b8ecc945a67d3 2196.0 1378.0 NaN NaN