How to select the lines in DataFrame which column value "year" is 2023?
- User
How to select the lines in DataFrame which column value "year" is 2023?
Assistant
Here is how you can select the rows from a DataFrame where the value in the "year" column is 2023:
import pandas as pd df = pd.DataFrame({"year": [2022, 2023, 2023, 2024], "value": [1, 2, 3, 4]}) df_2023 = df[df["year"] == 2023] print(df_2023)The key steps:
- Filter the DataFrame by creating a boolean Series that is True where
df["year"] == 2023 - Use this boolean Series to index and select matching rows from the DataFrame -
df[boolean_series]
The output will be a new DataFrame containing only the rows where the "year" column equals 2023:
year value 1 2023 2 2 2023 3The boolean indexing selects only rows where the Series is True, filtering the DataFrame to only those rows where the year is 2023.
- Filter the DataFrame by creating a boolean Series that is True where
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.