create a new dataframe by slicing the o3 rows. in python

To slice rows in a Pandas dataframe based on a condition, we can use boolean indexing with the .loc accessor. Here's an example of how to create a new dataframe by slicing the rows where the 'o3' column is greater than 50:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'o3': [10, 20, 30, 40, 50, 60, 70, 80, 90]})

# slice rows where o3 > 50 and create a new dataframe
new_df = df.loc[df['o3'] > 50]

# print the new dataframe
print(new_df)

# output:
#     o3
# 5   60
# 6   70
# 7   80
# 8   90
296 chars
18 lines

In this example, we create a new dataframe new_df by slicing the rows of the original dataframe df where the 'o3' column is greater than 50 using boolean indexing with .loc.

related categories

gistlibby LogSnag