0

I'm trying to pivot my pandas dataset. Here's my sample:

sample = {'Country' : ["US","US","US","UK","UK","UK"],
          "Indicator": ["Tax","Import","Export","Tax","Import","Export"],
          "2019":[1,2,3,4,5,6],
          "2020":[7,8,9,10,11,12],
          "2021":[13,14,15,16,17,18]}
sample_df = pd.DataFrame(sample)

And I'm trying to get the data in the following structure:

target = {"Country": ["US","US","US","UK","UK","UK"],
          "Date": ["2019","2020","2021","2019","2020","2021"],
          "Tax": [1,7,13,4,10,16],
          "Import": [2,8,14,5,11,17],
          "Export": [3,9,15,6,12,18]}
target_df = pd.DataFrame(target)
weirdaxe
  • 1
  • 1
  • Welcome back to Stack Overflow. For future reference please keep in mind that [you are expected to do some research before asking](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) - it's very easy to do. I literally copied and pasted your question title [into a search engine](https://duckduckgo.com/?q=Python+Pands+Pivot+Dataframe) and the third result was another duplicate - even though I didn't use Google *and* even though Pandas was misspelled when I did it. That duplicate links to the first one. – Karl Knechtel Nov 05 '21 at 13:32
  • The first result was for the [documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html) for the relevant tool. The second result was for a [tutorial site](https://www.geeksforgeeks.org/python-pandas-pivot/). – Karl Knechtel Nov 05 '21 at 13:33
  • It's also a good idea to keep in mind [ask], and *actually ask the question* using a question word like "how" or "what" and a question mark (`?`). – Karl Knechtel Nov 05 '21 at 13:34

0 Answers0