pandas - pivot_table with non-numeric values? (DataError: No numeric types to aggregate)

后端 未结 2 2000
-上瘾入骨i
-上瘾入骨i 2021-02-20 12:55

I\'m trying to do a pivot of a table containing strings as results.

import pandas as pd

df1 = pd.DataFrame({\'index\' : range(8),
\'variable1\' : [\"A\",\"A\",\         


        
2条回答
  •  耶瑟儿~
    2021-02-20 13:39

    I think the best compromise is to replace on/off with True/False, which will enable pandas to "understand" the data better and act in an intelligent, expected way.

    df2 = df1.replace({'on': True, 'off': False})
    

    You essentially conceded this in your question. My answer is, I don't think there's a better way, and you should replace 'on'/'off' anyway for whatever comes next.

    As Andy Hayden points out in the comments, you'll get better performance if you replace on/off with 1/0.

提交回复
热议问题