简体   繁体   中英

Python - how to create new columns in a dataframe from the unique values from an existing column with corresponding values?

small code is...

import pandas as pd

#INPUT FILE INFORMATION 
path = 'C:\Users\BDomitz\Desktop\Python\Stack_Example.xlsx'
sheet = "Sheet1"

#READ FILE
dataframe = pd.io.excel.read_excel(path, sheet)

the output for my current dataframe...

   date       animals       quantity
0  2015-02-10    dogs       1
1  2015-02-11    cats       2
2  2015-02-11    pigs       5

what I would like it to look like...

   date       animals       quantity    dogs   cats    pigs
0  2015-02-10    dogs       1            1      0        0
1  2015-02-11  cats, pigs   2            0      2        5

I would appreciate the help.

Starting from your dataframe:

In [9]: df
Out[9]:
         date animals  quantity
0  2015-02-10    dogs         1
1  2015-02-11    cats         2
2  2015-02-11    pigs         5

You can use the pivot method specifying which columns should be used as the index, as the column names, and as the values:

In [10]: df.pivot(index='date', columns='animals', values='quantity').fillna(0)
Out[10]:
animals     cats  dogs  pigs
date
2015-02-10     0     1     0
2015-02-11     2     0     5

This gets you the desired output, apart from the 'animals' and 'quantity' columns. Are they needed to be there?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM