Creating views on Pandas DataFrame

Many times while doing data analysis we are dealing with a large data set, having a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let’s see how we can create views on the Dataframe and select only those columns that we need and leave the rest.

For link to the CSV file used in the code, click here.

Solution #1: A set of columns in the DataFrame can be selected by dropping all those columns which are not needed.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Reading the csv file
df = pd.read_csv("nba.csv")
  
# Print the dataframe
print(df)

chevron_right


Output :

Now we will select all columns except the first three columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# drop the first three columns
df.drop(df.columns[[0, 1, 2]], axis = 1)

chevron_right


Output :

We can also use the names of the column to be dropped.

filter_none

edit
close

play_arrow

link
brightness_4
code

# drop the 'Name', 'Team' and 'Number' columns
df.drop(['Name', 'Team', 'Number'], axis = 1)

chevron_right


Output :

 
Solution #2 : We can individually select all those columns which we need and leave out the rest.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Reading the csv file
df = pd.read_csv("nba.csv")
  
# select the first three columns
# and store the result in a new dataframe
df_copy = df.iloc[:, 0:3]
  
# Print the new DataFrame
df_copy

chevron_right


Output :

We can also select the columns in a random manner by passing a list to the DataFrame.iloc attribute.

filter_none

edit
close

play_arrow

link
brightness_4
code

# select the first, third and sixth columns
# and store the result in a new dataframe
# The numbering of columns begins from 0
df_copy = df.iloc[:, [0, 2, 5]]
  
# Print the new DataFrame
df_copy

chevron_right


Output :

Alternatively, we can also name the columns that we want to select.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Select the below listed columns
df_copy = df[['Name', 'Number', 'College']]
  
# Print the new DataFrame
df_copy

chevron_right


Output :



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.