gpt4 book ai didi

python - 将 pandas DataFrame 的索引增加一个

转载 作者:太空狗 更新时间:2023-10-30 01:54:10 25 4
gpt4 key购买 nike

我想让我的数据框以索引 1 而不是 0 开头。但不知何故我没有得到它:

In[1]: df = pd.DataFrame([[4,7],[10,11],[7,2]],columns=['one', 'two'])
In[2]: df
Out[2]:
one two
0 4 7
1 10 11
2 7 2
In[3]: df.reindex(range(1,len(df)+1))
Out[3]:
one two
1 10 11
2 7 2
3 NaN NaN

我的第一行去哪儿了?我对 reindex() 有什么误解?

最佳答案

要将索引增加 1,您可以像这样简单地修改索引,df.index += 1

完整示例:

In [1]: import pandas as pd

In [2]: df = pd.DataFrame([[4,7],[10,11],[7,2]],columns=['one', 'two'])

In [3]: df
Out[3]:
one two
0 4 7
1 10 11
2 7 2

In [4]: df.index += 1

In [5]: df
Out[5]:
one two
1 4 7
2 10 11
3 7 2

关于python - 将 pandas DataFrame 的索引增加一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30268270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com