pandas python, make a periodic index from the columns "Year" and "Month"
- User
pandas python, make a periodic index from the columns "Year" and "Month"
- Assistant
You can use pandas’
PeriodIndexby passing the "Year" and "Month" columns along with the desired frequency. For a monthly period, for example, you can do the following:pythonimport pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Year': [2020, 2020, 2021, 2021], 'Month': [1, 2, 1, 2], 'Value': [10, 20, 30, 40] }) # Create a PeriodIndex using the "Year" and "Month" columns with a monthly frequency ('M') df.index = pd.PeriodIndex(year=df['Year'], month=df['Month'], freq='M') print(df)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.