Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Data Science by (17.6k points)

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')


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

pos0 = 0

pos1 = 1

pos2 = 2

colname = df.columns[pos0]


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

colname = df.columns[pos1]


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

colname = df.columns[pos2]


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)


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'])


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

Any help is greatly appreciated...

1 Answer

0 votes
by (41.4k points)

For deleting the single column, we can use:

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

For deleting more than 1 column:

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

And for deleting the entire df you can use:

del df


df = None

Learn Python with the help of this Python Course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories