35

I have a CSV file that is as the following:

index,Avg,Min,Max
Build1,56.19,39.123,60.1039
Build2,57.11,40.102,60.2
Build3,55.1134,35.129404123,60.20121

Based off my question here I am able to add some relevant information to this csv via this short script:

import pandas as pd

df = pd.read_csv('newdata.csv')
print(df)

df_out = pd.concat([df.set_index('index'),df.set_index('index').agg(['max','min','mean'])]).rename(index={'max':'Max','min':'Min','mean':'Average'}).reset_index()

with open('newdata.csv', 'w') as f:
    df_out.to_csv(f,index=False)

This results in this CSV:

index,Avg,Min,Max
Build1,56.19,39.123,60.1039
Build2,57.11,40.102,60.2
Build3,55.1134,35.129404123,60.20121
Max,57.11,40.102,60.20121
Min,55.1134,35.129404123,60.1039
Average,56.1378,38.1181347077,60.16837

I would like to now have it so I can update this csv. For example if I ran a new build (build4 for instance) I could add that in and then redo the Max, Min, Average rows. My idea is that I therefore delete the rows with labels Max, Min, Average, add my new row, redo the stats. I believe the code I need is as simple as (just for Max but would have lines for Min and Average as well):

df = pd.read_csv('newdata.csv')
df = df.drop('Max')

However this always results in an ValueError: labels ['Max'] not contained in axis

I have created the csv files in sublime text, could this be part of the issue? I have read other SO posts about this and none seem to help my issue.

I am unsure if this allowed but here is a download link to my csv just in case something is wrong with the file itself.

I would be okay with two possible answers:

  1. How to fix this drop issue
  2. How to add more builds and update the statistics (a method without drop)
3
  • df.drop('Max', axis=1) - default axis is 0? Commented Jul 5, 2017 at 16:44
  • 4
    I wonder why this was marked as a duplicate. The linked question is clearly different. Commented Feb 2, 2019 at 15:17
  • I had a similar error and it was because I had the wrong delimiter. I believe yours is fine because it defaults to comma but if you have some other delimiter then make sure you specify that on the read_csv call. i.e. delimiter=';' Commented Aug 29, 2024 at 1:13

2 Answers 2

46

You must specify the axis argument. default is axis = 0 which is rows columns is axis = 1.

so this should be your code.

df = df.drop('Max',axis=1)

edit: looking at this piece of code:

df = pd.read_csv('newdata.csv')
df = df.drop('Max')

The code you used does not specify that the first column of the csv file contains the index for the dataframe. Thus pandas creates an index on the fly. This index is purely a numerical one. So your index does not contain "Max".

try the following:

df = pd.read_csv("newdata.csv",index_col=0)
df = df.drop("Max",axis=0)

This forces pandas to use the first column in the csv file to be used as index. This should mean the code works now.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry there must be a bit of confusion as I have both Max as a column and a row. I would like to drop the row. df.drop('Max', axis=1) works as intended but when I do df.drop('Max', axis=0) I get an error.
@Abdall Answer updated. It should work now I believe.
goodness so many little things to keep track off. Sorry for the confusion but thank you for helping me understand your solution as well. Works as intended.
9

To delete a particular column in pandas; do simply:

del df['Max']

1 Comment

I tried the methods suggested above by fixing the index but was still running into the same issue. KeyError: "['xyz'] not found in axis". However, this fixed the issue for me. What exactly I did differently?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.