我正在尝试在数据帧中获取每组记录的第95位

时间:2019-05-01 17:07:27

标签: pandas quantile

我正在尝试获取数据帧中每组行的第95位。我试过了: mdf = mdf.groupby('GroupID')。quantile(.95)

,但是解释器返回错误: ValueError:“ GroupID”既是索引级别又是列标签,其含义不明确。

我有三列,我希望每个组的利用率为95: GroupID,时间戳,实用程序

以下代码:

#pandas 95th percentile calculator

import pandas as pd
import numpy as np

#pd.set_option('display.max_columns', 8)

cfile = "path"
rfile = "path"

#define columns in corereport dataframe
cdf = pd.read_csv(cfile, skiprows = 1, names = ['ID','Device','Bundle','IsPolled','Status','SpeedIn','SpeedOut','Timestamp','MaxIn','MaxOut'])

#drop specified columns from dataframe
to_drop = ['Device', 'Bundle', 'IsPolled', 'Status', 'SpeedIn', 'SpeedOut']
cdf.drop(to_drop, inplace=True, axis=1)

#define columns in relationship dataframe
rdf = pd.read_csv(rfile, skiprows = 1, names = ['GroupID', 'ID', 'Path', 'LowestBW', 'TotalBW'])

#merge the two dataframes together on the ID field
mdf = pd.merge(cdf, rdf, left_on='ID', right_on='ID', how = 'left')

#print(mdf.head())

#Add a column with the larger of two values of MaxIn and MaxOut for each row
mdf.loc[mdf['MaxIn'] > mdf['MaxOut'], 'Util'] = mdf['MaxIn']
mdf.loc[mdf['MaxIn'] < mdf['MaxOut'], 'Util'] = mdf['MaxOut']

#drop specified columns from data frame
to_drop = ['ID', 'MaxIn', 'MaxOut', 'Path', 'LowestBW', 'TotalBW']
mdf.drop(to_drop, inplace=True, axis=1)

#print(mdf.head().values)

#Group by the GroupID and Timestamp Columns and sum the value in Util
mdf = mdf.groupby(['GroupID', 'Timestamp'])['Util'].sum().reset_index()

#Grouping by GroupID and then sorting ascending
mdf = mdf.groupby(['GroupID']).apply(lambda x: x.sort_values(['Util']))

mdf = mdf.groupby('GroupID').quantile(.95)

#Write new dataframe out to a csv
ofile = 'path'
mdf.to_csv(ofile, encoding='utf-8', index=False)

1 个答案:

答案 0 :(得分:0)

问题在这里:

mdf = mdf.groupby(['GroupID']).apply(lambda x: x.sort_values(['Util']))

'GroupID'设置为mdf的索引。请尝试:

mdf = (mdf.groupby(['GroupID'])[['Timestamp', 'Util']]
       .apply(lambda x: x.sort_values(['Util'])))

mdf.sort_values(['GroupID', 'Util'], inplace=True)

但是,我相信您不需要对quantile的值进行排序。