简体   繁体   中英

Changing pandas index to column headings

I have a dataframe with my index as years, and one column of integer entries. I want to change my index into column headings. I have this structure for several small dataframes, which I will attach to a larger dataframe.

         1
0         
2021  4365
2020  5812
2019  6773
2018  6681
2017  6809
2016  6776
2015  6587
2014  5978

How can I turn this into:

       2021 2020 2019 2018 2017 2016 2015 2014
Amount 4365 5812 6773 6681 6809 6776 6587 5978

I'm afraid I don't know where to begin, and I can't find any examples of this online. Any help would be greatly appreciated.

I have your df :

print(df)

         1
0         
2021  4365
2020  5812
2019  6773
2018  6681
2017  6809
2016  6776
2015  6587
2014  5978

And used T to transpose, and pandas.DataFrame.rename the index.

res = df.T
res.rename(index={1: "Amount"},inplace=True)

print(res)

        2021  2020  2019  2018  2017  2016  2015  2014
Amount  4365  5812  6773  6681  6809  6776  6587  5978

Perhaps this gets you what you need.

Did you transpose your dataframe?

import pandas as pd

df = pd.DataFrame([1,2],[3,4])
print(df.T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM