Я пытаюсь получить 95-й квантиль для каждой группы строк в кадре данных.Я пытался: mdf = mdf.groupby ('GroupID'). Quantile (.95)
, но интерпретатор возвращает ошибку: ValueError: 'GroupID' является и уровнем индекса, и меткой столбца, котораянеоднозначный.
У меня есть три столбца, и я хочу 95-е использование для каждой группы: GroupID, Timestamp, Util
Код ниже:
#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)