pandas :索引更新和更改按位置访问的值 [英] Pandas: Index updating and changing value accessed by location
问题描述
关于Python Pandas数据框,我有两个与索引相关的问题.
I have two index-related questions on Python Pandas dataframes.
import pandas as pd
import numpy as np
df = pd.DataFrame({'id' : range(1,9),
'B' : ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'amount' : np.random.randn(8)})
df = df.ix[df.B != 'three'] # remove where B = three
df.index
>> Int64Index([0, 1, 2, 4, 6, 7], dtype=int64) # the original index is preserved.
1)我不理解为什么修改数据框后索引不会自动更新.有没有一种方法可以在修改数据框时自动更新索引?如果没有,最有效的手动方法是什么?
1) I do not understand why the indexing is not automatically updated after I modify the dataframe. Is there a way to automatically update the indexing while modifying a dataframe? If not, what is the most efficient manual way to do this?
2)我希望能够将df
的第5个元素的B
列设置为三个".但是df.iloc[5]['B'] = 'three'
不会那样做.我检查了手册,但没有涵盖如何更改通过位置访问的特定单元格值.
2) I want to be able to set the B
column of the 5th element of df
to 'three'. But df.iloc[5]['B'] = 'three'
does not do that. I checked on the manual but it does not cover how to change a specific cell value accessed by location.
如果按行名访问,可以执行:df.loc[5,'B'] = 'three'
,但是我不知道什么是等效的索引访问.
If I were accessing by row name, I could do: df.loc[5,'B'] = 'three'
but I don't know what the index access equivalent is.
P.S. Link1 和
P.S. Link1 and link2 are relevant answers to my second question. However, they do not answer my question.
推荐答案
1)我不明白为什么修改数据框后索引不会自动更新.
1) I do not understand why the indexing is not automatically updated after I modify the dataframe.
如果要在删除/添加行后重置索引,可以执行以下操作:
If you want to reset the index after removing/adding rows you can do this:
df = df[df.B != 'three'] # remove where B = three
df.reset_index(drop=True)
B amount id
0 one -1.176137 1
1 one 0.434470 2
2 two -0.887526 3
3 two 0.126969 5
4 one 0.090442 7
5 two -1.511353 8
索引是用来对行进行标签/标记/标识的...所以您可能会考虑将"id"列作为索引,然后您将欣赏到熊猫在删除时不会自动更新"索引行.
Indexes are meant to label/tag/id a row... so you might think about making your 'id' column the index, and then you'll appreciate that Pandas doesn't 'automatically update' the index when deleting rows.
df.set_index('id')
B amount
id
1 one -0.410671
2 one 0.092931
3 two -0.100324
4 three 0.322580
5 two -0.546932
6 three -2.018198
7 one -0.459551
8 two 1.254597
2)我希望能够将df的第5个元素的B列设置为三".但是df.iloc [5] ['B'] ='三'不会那样做.我查看了手册,但没有涵盖如何更改通过位置访问的特定单元格值.
2) I want to be able to set the B column of the 5th element of df to 'three'. But df.iloc[5]['B'] = 'three' does not do that. I checked on the manual but it does not cover how to change a specific cell value accessed by location.
杰夫已经回答了这个问题...
Jeff already answered this...
这篇关于 pandas :索引更新和更改按位置访问的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!