python Pandas groupby方法

时间:2016-03-23 21:55:59

标签: pandas

我在使用带有pandas模块的groupby函数时遇到了问题。我收到错误:DataError:没有要聚合的数字类型

我不确定我在做错了数据框中有数字数据。

以下是我的代码:

lte_columns = ['Period start','Period end','zone','usid','site id','rank','Total LCQI Impact','LTE BLOCK Impact','LTE DROP Impact','LTE TPUT Impact','engineer notes']
#lte_df = pd.DataFrame(dtype=float)
lte_df = pd.DataFrame(dtype=float)

## iterate over the CQI impact file seperate LTE from UMTS and perform lookup for each Technology/USID
testFile = "sample_CSCT_CQI_IMPACT_Greater Midwest_20160305_20160311.xls"
df = pd.read_excel(testFile,sheetname="Sheet1")

weekBegin = df['Date'].min()
weekEnd = df['Date'].max()

## update new dataFrames while iterating over input dataframe

for idx, row in df.iterrows():
    usid = row['USID']
    region, zone = row['District & Zone'].split('-')
    if usid in lte_lookup:
        site_id = lte_lookup[usid][1]
    else:
        site_id = "N/A"

    lte = pd.Series([weekBegin,weekEnd,zone,usid,site_id,'0','0','0','0','0','0'])
    lte_df = lte_df.append(lte,ignore_index=True)

lte_df.columns = lte_columns
grps = lte_df.groupby(['usid'])
avgs = grps.mean()
avgs.to_excel("pandas_out.xlsx",merge_cells=False) 

print "done"

以下是lte_df的示例:

>>> print lte_df
     Period start  Period end zone      usid    site id rank Total LCQI Impact LTE BLOCK Impact LTE DROP Impact LTE TPUT Impact engineer notes
0      03/05/2016  03/11/2016  69E   56788.0   MOL02607    0                 0                0               0               0              0
1      03/05/2016  03/11/2016  70F   58438.0   KSL05065    0                 0                0               0               0              0
2      03/05/2016  03/11/2016  69A  120595.0  MOL00531W    0                 0                0               0               0              0
3      03/05/2016  03/11/2016  70D   75566.0   KSL04272    0                 0                0               0               0              0
4      03/05/2016  03/11/2016  70F   58454.0   KSL05106    0                 0                0               0               0              0
5      03/05/2016  03/11/2016  70E   41793.0   KSL04151    0                 0                0               0               0              0
6      03/05/2016  03/11/2016  70C    9500.0   KSL06382    0                 0                0               0               0              0
7      03/05/2016  03/11/2016  69A   56586.0   MOL01143    0                 0                0               0               0              0


>>> lte_df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 6565 entries, 0 to 6564
Data columns (total 11 columns):
Period start         6565 non-null object
Period end           6565 non-null object
zone                 6565 non-null object
usid                 6565 non-null float64
site id              6565 non-null object
rank                 6565 non-null object
Total LCQI Impact    6565 non-null object
LTE BLOCK Impact     6565 non-null object
LTE DROP Impact      6565 non-null object
LTE TPUT Impact      6565 non-null object
engineer notes       6565 non-null object
dtypes: float64(1), object(10)
memory usage: 615.5+ KB
>>>

1 个答案:

答案 0 :(得分:0)

根据您在DataFrame中可用的数据,您的groupby无法正常工作,因为您的代码正在尝试确定列的平均值,但它不能,因为它们不是浮点数。即使你在其他列中的零也是字符串。

所以这不起作用:

grps = lte_df.groupby(['usid'])
avgs = grps.mean()

但是例如

grps = lte_df[['Period start', 'usid']].groupby(['Period start'])
avgs = grps.mean()

将工作,因为它是由一些列分组,唯一其他剩余的列是一个浮点数,然后将返回一些东西。我意识到这不是你想要做的,但它是一个如何运作的例子。