w3resource

Pandas: Create a smaller dataframe with a subset of all features


8. Create a Subset DataFrame

Write a Pandas program to create a smaller dataframe with a subset of all features.

Sample Solution:

Python Code :

import pandas as pd
df = pd.read_csv('movies_metadata.csv')
# Create a smaller dataframe
small_df = df[['title', 'release_date', 'budget', 'revenue', 'runtime']]
print("Smaller DataFrame:")
print(small_df.head())

Sample Output:

Smaller DataFrame:
                         title release_date    budget    revenue  runtime
0                    Toy Story   1995-10-30  30000000  373554033     81.0
1                      Jumanji   1995-12-15  65000000  262797249    104.0
2             Grumpier Old Men   1995-12-22         0          0    101.0
3            Waiting to Exhale   1995-12-22  16000000   81452156    127.0
4  Father of the Bride Part II   1995-02-10         0   76578911    106.0	                                       

For more Practice: Solve these Related Problems:

  • Write a Pandas program to load movies_metadata.csv and create a smaller DataFrame with only the columns title, genres, and release_date.
  • Write a Pandas program to extract a subset of movies_metadata.csv containing only numeric columns and the title.
  • Write a Pandas program to select a random sample of 100 rows from movies_metadata.csv and a few key columns.
  • Write a Pandas program to filter movies_metadata.csv for movies released after 2000 and create a subset DataFrame with selected features.

Python-Pandas Code Editor:

Sample Table:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to get the details of fifth movie of the DataFrame.
Next: Write a Pandas program to display the first 10 rows of the DataFrame.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.