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
To select rows in a Pandas DataFrame with a specific value in the "year" column, you can use the DataFrame's
locorquerymethods. Here's an example using both methods:import pandas as pd # Sample data df = pd.DataFrame({ 'year': [2019, 2021, 2022, 2023], 'value1': [100, 112, 130, 150], 'value2': [50, 55, 57, 60], }) # Filter using the `loc` method filtered_df_loc = df.loc[df['year'] == 2023] # Filter using the `query` method filtered_df_query = df.query("year == 2023")Both
filtered_df_locandfiltered_df_querywill contain only the rows with the "year" value of 2023.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.