How to create a subset of a DataFrame in Pandas
• 0 minTo create a subset of a Pandas DataFrame, you can use the indexing operator []
and provide the desired columns or a condition based on the values of one or more columns.
Here's an example:
import pandas as pd
df = pd.read_csv('data.csv')
# Selecting specific columns
subset = df[['column1', 'column2']]
# Selecting rows based on a condition
subset = df[df['column3'] > 0.5]
In the first example, the subset contains only the columns column1
and column2
of the original DataFrame df
. In the second example, the subset contains only the rows of df
where the value of column3
is greater than 0.5.
You can also use the .loc
and .iloc
methods to select subsets of a DataFrame based on labels and integer positions, respectively.