logo_learn_stats

Pandas: Loose Explicit Column when Uploading CSV Report

Posted on
banner 336x280

You’ll virtue please see ordinary syntax to loose a selected column when uploading a CSV document right into a pandas DataFrame:

df = pd.read_csv('basketball_data.csv', usecols=lambda x: x != 'rebounds')

This actual instance will learn each and every column from a CSV document referred to as basketball_data.csv right into a pandas DataFrame with the exception of for the column referred to as rebounds.

banner 468x60

Refer to instance displays learn how to virtue this syntax in apply.

Instance: Loose Explicit Column when Uploading CSV Report in Pandas

Assume now we have please see CSV document referred to as basketball_data.csv:

We will be able to virtue please see syntax to import the CSV document into pandas and loose the column referred to as rebounds when uploading:

import pandas as pd

#import all columns with the exception of 'rebounds' into DataFrame
df = pd.read_csv('basketball_data.csv', usecols=lambda x: x != 'rebounds')

#view ensuing DataFrame
print(df)

  crew  issues
0    A      22
1    B      14
2    C      29
3    D      30

Understand that the rebounds column was once dropped once we imported the CSV document into pandas.

If you want to loose a couple of columns when uploading, you’ll virtue the now not in operator as follows:

import pandas as pd

#import all columns with the exception of 'crew' and 'rebounds' into DataFrame
df=pd.read_csv('basketball_data.csv', usecols=lambda x: x now not in ['team', 'rebounds'])

#view ensuing DataFrame
print(df)

   issues
0      22
1      14
2      29
3      30

Understand that the crew and rebounds columns had been each dropped once we imported the CSV document into pandas.

Notice that you’ll come with as many column names as you’d like within the checklist following the now not in operator to loose as many columns as you’d like when uploading a CSV document.

Spare Sources

Refer to tutorials provide an explanation for learn how to carry out alternative ordinary duties in Python:

Pandas: Skip Rows when Studying CSV Report
Pandas: Append Information to Present CSV Report
Pandas: Worth read_csv with usecols Argument

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *