3

How do I delete a column from a DataFrame? I know this data is not reproducible as I have a CSV file and I am trying to build a pandas data frame to do some wrangling.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('C:\LoadProfiles\CSV\WillBaySchl 2013_2014 KW.csv')

print(df)

This will return the head/tail and:[34944 rows x 3 columns]

pos0 = 0
pos1 = 1
pos2 = 2

colname = df.columns[pos0]
print(colname)

This will return: Meter ID (I want to drop this column/dataframe)

colname = df.columns[pos1]
print(colname)

This will return: Date / Time (I want this to be the pd data frame index)

colname = df.columns[pos2]
print(colname)

This will return: KW(ch: 1 set:0) (This is the data that I want to rename "kW" and do some wrangling...)

If I try this code below:

df = pd.DataFrame.drop(['Meter ID'], axis=1)

print(df)

Python will return the error:TypeError: drop() missing 1 required positional argument: 'labels'

If I try this code below:

df = pd.DataFrame.drop(columns=['Meter ID'])
print(df)

Python will return the error: TypeError: drop() got an unexpected keyword argument 'columns'

Any help is greatly appreciated...

3
  • 2
    You probably have a pandas pre- 0.21, where the columns kw was introduced. Check your version and use labels, coupled with axis=1 instead. Commented Dec 7, 2017 at 22:25
  • 3
    pd.DataFrame.drop(['Meter ID'], axis=1) you are calling the method on the DataFrame constructor so it thinks the first positional argument is self. Use it on an instance (for example df). Commented Dec 7, 2017 at 22:26
  • Can reiterate what you mean by self? I don't understand how to put this to use with Pandas.. Thanks Commented Dec 8, 2017 at 14:07

4 Answers 4

12

If I understand right to delete column (single) you should use:

df = pd.DataFrame.drop('Meter ID', axis=1)

For more than 1 column:

df = pd.DataFrame.drop(['Meter ID', 'abc'], axis=1)

Difference is in [] brackets.

To delete the whole df you can use either (as mentioned already):

del df

or

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

Comments

2

After reading your question , what i understand is you wanted to drop column ['Meter ID'] available in your df = pd.read_csv('C:\LoadProfiles\CSV\WillBaySchl 2013_2014 KW.csv') pandas dataframe . I am assuming that you have column name ['Meter ID'] like these in your dataframe and also as header in your csv file .

>>> df.dtypes
Meter ID           int64
someothercolumn    int64
dtype: object

for that you can simply use these code ,

del df['Meter ID']

Now if you wanted to delete overall dataframe you can simply use these code,

df=None

Comments

1

To drop a column from dataframe,

df = df.drop('Meter ID', axis=1)

Drop more than one columns at a time,

df = df.drop(['Meter ID', 'SomethingElse'], axis=1)

For more pandas.DataFrame.drop

Comments

0

To lose the 'Meter ID' column you could also use:

df = df.drop(columns=['Meter ID']) 

post pandas version 0.21

Comments

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.