Reputation: 155
I found weird behavior of pandas when converting data frame to pivot table.
import pandas as pd
df = pd.DataFrame({'car_id': {0: 'Trabant', 1: 'Buick', 2: 'Dodge'}, 'car_order': {0: 2, 1: 1, 2: 14}, 'car_name': {0: 'Trabant', 1: 'Buick', 2: 'Dodge'}, 'car_rank': {0: 111111317.29, 1: 1111112324.0, 2: 1111112324.5}})
table = df.pivot_table(index=['car_id', 'car_name', 'car_order'], columns=[],values=['car_rank'], fill_value='',dropna=True)
print table
df1 = pd.DataFrame({'car_id': {0: 'Trabant', 1: 'Buick', 2: 'Dodge'}, 'car_order': {0: 2, 1: 1, 2: 14}, 'car_name': {0: 'Trabant', 1: 'Buick', 2: 'Dodge'}, 'car_rank': {0: 17.29, 1: 24.0, 2: 24.5}})
table1 = df1.pivot_table(index=['car_id', 'car_name', 'car_order'], columns=[],values=['car_rank'], fill_value='',dropna=True)
print table1
Result output:
Table
car_rank
car_id car_name car_order
Buick Buick 1 1111112324
Dodge Dodge 14 1111112324
Trabant Trabant 2 111111317
Table 1
car_rank
car_id car_name car_order
Buick Buick 1 24.00
Dodge Dodge 14 24.50
Trabant Trabant 2 17.29
Do you know why in Table are values converted to int and for Table 1 values stay as float?
pandas 0.18.0 , python 2.7.9
Upvotes: 5
Views: 7657
Reputation: 210852
here is result of my observations of pandas 0.18.0
:
Source code of pandas/tools/pivot.py
definition of pivot_table()
lines: 141-142:
if fill_value is not None:
table = table.fillna(value=fill_value, downcast='infer')
This is exactly what happened to your pivoted DF:
In [78]: df.fillna('', downcast='infer')
Out[78]:
car_id car_name car_order car_rank
0 Trabant Trabant 2 111111317
1 Buick Buick 1 1111112324
2 Dodge Dodge 14 1111112324
Types:
In [48]: df.fillna('', downcast='infer').dtypes
Out[48]:
car_id object
car_name object
car_order int64
car_rank int64
dtype: object
Interestingly enough - if you use pivot_table()
properly (i.e. for pivoting) - it works correctly:
In [81]: df.pivot_table(index=['car_id', 'car_order'], columns=['car_name'], values=['car_rank'],dropna=True, fill_value='')
Out[81]:
car_rank
car_name Buick Dodge Trabant
car_id car_order
Buick 1 1111112324.00
Dodge 14 1111112324.50
Trabant 2 111111317.29
PS I still can't understand why are you using pivot_table in that strange way - what are you going to achieve?
Upvotes: 6