简体   繁体   中英

Pandas 0.18 how to pivot data frame when the data contains both numeric and non numeric types

import pandas as pd

df1 = pd.DataFrame({'index': range(6),
                    'Name': ["Swap1", "Swap2", "Swap3", "Swap1", "Swap2", "Swap3"],
                    'LegName': ["pay", "receive", "total", "pay", "receive", "total"],
                    'Metric': ["pv", "pv", "pv", "start", "start", "start"],
                    'result': [1, 2, 3, "1y", "1y", "1y"]})

print(df1)

The results column contains both numeric and non numeric types. aggfunc=lambda x: x used to work with pandas 0.16 and 0.17 but is failing with 0.18. aggfunc=lambda x: sum(x) will work when all data is numeric and aggfunc=lambda x: ' '.join(x) will work when all data is non numeric. But I am stuck with a dataset when it is both numeric and non numeric. Not sure how to include a condition with the aggfunc. All entries have a unique value. So no aggregation is actually required.

print(df1.pivot_table(values='result', index='index',
                      columns=['Name', 'LegName', 'Metric'],
                      aggfunc=lambda x:x))

print(df1.pivot_table(values='result', index='index',
                      columns=['Name', 'LegName', 'Metric'],
                      aggfunc=lambda x: sum(x)))

print(df1.pivot_table(values='result', index='index',
                      columns=['Name', 'LegName', 'Metric'],
                      aggfunc=lambda x: ' '.join(x)))

this will work:

In [32]: df1.pivot_table(values='result', index='index',
   ....:                 columns=['Name', 'LegName', 'Metric'],
   ....:                 fill_value=0,
   ....:                 aggfunc='sum')
Out[32]:
Name    Swap1         Swap2       Swap3
LegName   pay       receive       total
Metric     pv start      pv start    pv start
index
0           1     0       0     0     0     0
1           0     0       2     0     0     0
2           0     0       0     0     3     0
3           0    1y       0     0     0     0
4           0     0       0    1y     0     0
5           0     0       0     0     0    1y

but it's not quite clear what do you want to achieve...

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