1

my dataset has some information by location for n dates. The CSV looks like

Country     year2018     year2019        year2020
          saleA saleB    SaleA SaleB     saleA saleB

USA        22   23       323    32        31    65
china      12   12        2     66        66    78

I want my data to be of the form

Country   year      saleA saleB 
             
USA       year2018   22    23    
USA       year2019   323   32
USA       year2020   31    65
china     year2018   12    12
.
.
.

How can I do it using pandas?

I tried using pd.melt but couldn't figured out.

3
  • 1
    df.set_index('Country').stack(0).reset_index()? Commented Jan 31, 2023 at 21:46
  • Please provide a reproducible example, your input is not a valid CSV nor a valid DataFrame Commented Jan 31, 2023 at 21:47
  • @mozway solved the issue with your approach, Thanks. Commented Jan 31, 2023 at 21:51

1 Answer 1

2

You can reshape your dataframe with set_index and stack:

out = (df.set_index('Country')
         .rename_axis(columns=['year', None])
         .stack('year').reset_index())

  Country      year  saleA  saleB
0     USA  year2018     22     23
1     USA  year2019    323     32
2     USA  year2020     31     65
3   China  year2018     12     12
4   China  year2019      2     66
5   China  year2020     66     78

Another solution with melt and pivot_table:

>>> out = (df.melt(id_vars='Country', var_name=['year', 'sale'])
             .pivot_table(index=['Country', 'year'], columns='sale', values='value')
             .reset_index())
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.