[py]pandas数据统计学习

pandas.core.base.DataError: No numeric types to aggregate错误规避

我没有去解决这个问题, 而用填充0规避了这个问题

统计 聚合

d = [
    {'cur': 1, 'next': 2, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 3, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 4, 'avgtime': None, 'callcount': None},

    {'cur': 1, 'next': 2, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 3, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 4, 'avgtime': None, 'callcount': None},

    {'cur': None, 'next': 4, 'avgtime': None, 'callcount': None},
]

df = pd.DataFrame(d, dtype='int')
df.groupby(["cur", "next"], as_index=False).mean()

agg函数

使用这种聚合会卡到这个bug
pandas.core.base.DataError: No numeric types to aggregate错误规避

import pandas as pd

d = [
    {'cur': 1, 'next': 2, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 3, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 4, 'avgtime': None, 'callcount': None},

    {'cur': 1, 'next': 2, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 3, 'avgtime': None, 'callcount': None},
    {'cur': 2, 'next': 4, 'avgtime': None, 'callcount': None},

    {'cur': None, 'next': 4, 'avgtime': None, 'callcount': None},
]

df = pd.DataFrame(d, dtype='int')
g = df.groupby(["cur", "next"], as_index=False)
res = g.agg(
    {
        'avgtime': 'sum',
        'callcount': 'mean',
    }
)

猜你喜欢

转载自www.cnblogs.com/iiiiiher/p/10218994.html