pandas 更改数据框索引值,同时保持其他列数据不变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18371609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:06:07  来源:igfitidea点击:

Change dataframe index values while keeping other column data same

pythonpandas

提问by WittyID

I have a DataFrame with 4 columns and 251 rows and an index that is a progression of numbers e.g. 1000 to 1250 . The index was initially necessary to aid in joining data from 4 different dataframes. However, once i get the 4 columns together, i would like to change the index to a number progression from 250 to 0. This is because i would be performing the same operation on different sets of data (in groups of 4) that would have different indices, e.g. 2000 to 2250 or 500 to 750, but would all have the same number of rows. 250 to 0 is a way of unifying these data sets, but i can't figure out how to do this. i.e. i'm looking for something that replaces any existing index with the function range(250, 0, -1)

我有一个包含 4 列和 251 行的 DataFrame 和一个索引,它是数字的级数,例如 1000 到 1250 。该索引最初是帮助连接来自 4 个不同数据帧的数据所必需的。但是,一旦我将 4 列放在一起,我想将索引更改为从 250 到 0 的数字级数。这是因为我将对不同的数据集(以 4 为一组)执行相同的操作,这些数据集将有不同的索引,例如 2000 到 2250 或 500 到 750,但都具有相同的行数。250 到 0 是统一这些数据集的一种方式,但我不知道如何做到这一点。即我正在寻找用函数 range(250, 0, -1) 替换任何现有索引的东西

I've tried using set_index below and a whole bunch of other attempts that invariably return errors,

我已经尝试使用下面的 set_index 和一大堆其他总是返回错误的尝试,

df.set_index(range(250, 0, -1), inplace=True) 

and in the instance when i am able to set the index of the df to the range, the data in the 4 columns change to NaN since they have no data that matches the new index. I apologize if this is rudimentary, but i'm a week old in the world of python/pandas, haven't programmed in +10yrs, and have taken 2 days to try to figure this out for myself as an exercise, but its time to cry... Uncle!!

并且在我能够将 df 的索引设置为范围的情况下,4 列中的数据更改为 NaN,因为它们没有与新索引匹配的数据。如果这是基本的,我深表歉意,但我在 python/pandas 的世界里只有一个星期大,在 +10 年没有编程,并且花了 2 天的时间来尝试为自己解决这个问题作为练习,但它的时间哭了……叔叔!!

采纳答案by Marius

Try introducing the 250:0 indices as a column first, then setting them as the index:

尝试先将 250:0 索引作为列引入,然后将它们设置为索引:

df = pd.DataFrame({'col1': list('abcdefghij'), 'col2': range(0, 50, 5)})
df['new_index'] = range(30, 20, -1)
df.set_index('new_index')

Before:

前:

  col1  col2  new_index
0    a     0         30
1    b     5         29
2    c    10         28
3    d    15         27
4    e    20         26
5    f    25         25
6    g    30         24
7    h    35         23
8    i    40         22
9    j    45         21

After:

后:

          col1  col2
new_index           
30           a     0
29           b     5
28           c    10
27           d    15
26           e    20
25           f    25
24           g    30
23           h    35
22           i    40
21           j    45

回答by JoeCondron

You can just do

你可以做

df.index = range(250, 0, -1)

or am I missing something?

或者我错过了什么?