英文:
How do I rename a dataframe index and make it count from 1 without fragmenting the header?
问题
我想要一个从“1”开始的数据框,并且我想要重命名索引。
不管这些操作的顺序如何,我只是想确保标题不会分散。
这肯定是一个重复的问题,但我似乎找不到它!
这不起作用:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index += 1
df.rename_axis('rank')
也不起作用:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.rename_axis('rank')
df.index += 1
期望的结果:
rank    A	B	
   1	1	4
   2	2	5
   3	3	6
英文:
I want a dataframe where the index starts from 1. I also want to rename the index.
It doesn't matter what order these operations are performed, I just want to ensure that the header isn't fragmented.
This is surely a duplicate question, but I can't seem to find it(!)
This doesn't work:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index += 1
df.rename_axis('rank')
>>>
        A	B
rank		
   1	1	4
   2	2	5
   3	3	6
Nor does this:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.rename_axis('rank')
df.index += 1
>>>
	A	B
1	1	4
2	2	5
3	3	6
Desired result:
rank    A	B	
   1	1	4
   2	2	5
   3	3	6
答案1
得分: 1
In short, the index name is not a header and therefore will not be on the same line by default.
Option 1) Best for printing
Print you dataframe in another way, e.g. use df.to_markdown or best tabulate
from tabulate import tabulate
print(tabulate(df, headers=["rank"]+list(df.columns)))
Option 2)
You could make your own "index" column and "hide" the index, but in general that is not a good idea as the dataframe loses functionality!
Only use this for printing.
df["rank"] = df.index + 1
df.index = [""]*len(df)
Option 3)
you could slightly cheat by naming your first column rank but put only empty strings inside. In that case remember that you did it.
df["rank"] = ""
df.columns = ["rank"] + [c for c in df.columns if c != "rank"] # you can do this in other ways too
df.index += 1
There might be a print option for pandas, but I haven't seen it yet.
英文:
In short, the index name is not a header and therefore will not be on the same line by default.
Option 1) Best for printing
Print you dataframe in another way, e.g. use df.to_markdown or best tabulate
from tabulate import tabulate
print(tabulate(df, headers=["rank"]+list(df.columns)))
Option 2)
You could make your own "index" column and "hide" the index, but in general that is not a good idea as the dataframe loses functionality!
Only use this for printing.
df["rank"] = df.index + 1
df.index = [""]*len(df)
Option 3)
you could slightly cheat by naming your first column rank but put only empty strings inside. In that case remember that you did it.
df["rank"] = ""
df.columns = ["rank"] + [c for c in df.columns if c != "rank"] # you can do this in other ways too
df.index += 1
There might be a printoption for pandas, but I haven't seen it yet.
答案2
得分: 1
rename_axis 不是原地操作,而是返回一个新的 DataFrame。您需要将输出分配给一个变量:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index += 1
df = df.rename_axis('rank')
print(df)
如果您想要一条命令,可以使用 Index.rename:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index = df.index.rename('rank') + 1
print(df)
修改后的 df:
      A  B
rank      
1     1  4
2     2  5
3     3  6
如果您希望在同一级别上显示它,请使用 reset_index 和 to_string:
print(df.reset_index().to_string(index=False))
输出:
 rank  A  B
    1  1  4
    2  2  5
    3  3  6
英文:
rename_axis is not in place, but returns a new DataFrame. You would need to assign the output:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index += 1
df = df.rename_axis('rank')
print(df)
If you want a single command, use Index.rename:
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.index = df.index.rename('rank')+1
print(df)
Modified df:
      A  B
rank      
1     1  4
2     2  5
3     3  6
If you want to display it on the same level, use reset_index and to_string:
print(df.reset_index().to_string(index=False))
Output:
 rank  A  B
    1  1  4
    2  2  5
    3  3  6
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论