Step 1: Importing the Pandas Library
import pandas as pd
This line imports the pandas library, which is used for data manipulation and analysis in Python.
Step 2: Creating a Dictionary
data = {"A": [1, 2, 3]}
A dictionary named data is created with a single key "A" and a list of values [1, 2, 3].
Step 3: Creating a DataFrame
df = pd.DataFrame(data)
This converts the dictionary data into a pandas DataFrame.
The resulting DataFrame looks like this:
A
0 1
1 2
2 3
Step 4: Filtering the DataFrame
df[df["A"] > 1]
Here, df["A"] > 1 creates a Boolean condition:
0 False
1 True
2 True
Name: A, dtype: bool
This condition is applied to df, keeping only the rows where column "A" has values greater than 1.
The resulting DataFrame is:
A
1 2
2 3
Step 5: Printing the Filtered DataFrame
print(df[df["A"] > 1])
This prints the filtered output:
A
1 2
2 3
0 Comments:
Post a Comment