简体   繁体   English

将 pandas 索引更改为列标题

[英]Changing pandas index to column headings

I have a dataframe with my index as years, and one column of integer entries.我有一个 dataframe 索引为年份,以及一列 integer 条目。 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.我有几个小数据帧的这个结构,我将把它们附加到一个更大的 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 :我有你的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.并用T转置, pandas.DataFrame.rename .重命名索引。

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?你转置你的 dataframe 了吗?

import pandas as pd

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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