1

I have an aggregation function which totals rows in a certain column based on an ID. After being able to correctly aggregate my rows, I wanted to select only the relevant columns, but I keep getting an error saying my ID column isn't found.

Full Code:

import pandas as pd
  
# initialize list of lists
data = [['A29', 112, 10, 0.3], ['A29',112, 15, 0.1], ['A29', 112, 14, 0.22], ['A29', 88, 33, 0.09], ['A29', 88, 29, 0.1], ['A29', 88, 6, 0.2]]
  
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Id', 'Cores', 'Provisioning', 'Utilization'])

df['total'] = df['Provisioning'] * df['Utilization']

df=df[['Id', 'Cores','total']]
aggregation_functions = {'Cores': 'first', 'total': 'sum'}
df_new = df.groupby(df['Id']).aggregate(aggregation_functions)

df_new['total1']=df_new['total']/3
print(df_new) #the dataframe contains the Id columns
print(df_new.columns) #doesn't print Id column

df_new=df_new[['Id', 'total1']] #Error: Id column not found

I'm not sure what is happening here. A line above, I print the dataframe and the Id column is present. However, when I try selecting it, it returns an error saying it isn't found. How can I fix this issue?

1
  • 2
    The groupby makes the ID column into the index, so it is no longer a column. You can access it with df_new.index or use as_index=False Commented Feb 1, 2023 at 17:18

1 Answer 1

3

You should use as_index=False in the call to .groupby(); the Id column is part of the index, which prevents you from selecting it in the desired manner:

df_new = df.groupby(df['Id'], as_index=False).aggregate(aggregation_functions)
Sign up to request clarification or add additional context in comments.

2 Comments

df.groupby('Id', as_index=False).aggregate(aggregation_functions)?
That works, though I wanted to modify as little of the original code as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.