1

I have a dataframe with columns like this -

Name Id 2019col1 2019col2 2019col3 2020col1 2020col2 2020col3 2021col1 2021Ccol2 2021Ccol3

That is, the columns are repeated for each year. I want to take the year out and make it a column, so that the final dataframe looks like -

 Name Id Year col1 col2 col3

Is there a way in pandas to achieve something like this?

1 Answer 1

3

Use wide_to_long, but before change order years to end of columns names like 2019col1 to col12019 in list comprehension:

print (df)
  Name   Id  2019col1  2019col2  2019col3  2020col1  2020col2  2020col3  \
0    a  456         4         5         6         2         3         4   

   2021col1  2021col2  2021col3  
0         5         2         1  

df.columns = [x[4:] + x[:4] if x[:4].isnumeric() else x for x in df.columns]

df = (pd.wide_to_long(df.reset_index(), 
                      ['col1','col2', 'col3'],
                      i='index',
                      j='Year').reset_index(level=0, drop=True).reset_index())
print (df)

   Year   Id Name  col1  col2  col3
0  2019  456    a     4     5     6
1  2020  456    a     2     3     4
2  2021  456    a     5     2     1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.