pandas - 02 - 索引 - part1

本文详细介绍了Pandas库中DataFrame与Series的索引方法,包括loc、iloc及[]操作符的使用技巧,以及布尔索引、函数式索引等高级特性。同时,文章还探讨了快速标量索引、区间索引的创建与应用。

索引

单级索引

1.loc方法、iloc方法、[]操作符

loc方法

通过索引访问

(注意:所有在loc中使用的切片全部包含右端点!

单行索引
df.loc[1103]

返回第1103row

多行索引
df.loc[[1102,2304]]

返回第1102,2304row

df.loc[1304:].head()

返回从1304到最后一row

df.loc[2402::-1].head()

df行颠倒输出,从2404输出到第一行,步长为1

单列索引
df.loc[:,‘Height’].head()

返回df[‘Height’]的全部

多列索引
df.loc[:,[‘Height’,‘Math’]].head()

返回这两列

df.loc[:,‘Height’:‘Math’].head()

返回’Height’ 到’Math’的全部列,包括右端点

联合索引
df.loc[1102:2401:3,‘Height’:‘Math’].head()

1102到2401row,步长为3,即两个输出行之间隔2行

函数式索引
df.loc[lambda x:x[‘Gender’]==‘M’].head()

#loc中使用的函数,传入参数就是前面的df
使得x[‘Gender’]=='M’的x传入df

def f(x):
return [1101,1103]

df.loc[f]
返回1101 1103行

布尔索引
df.loc[df[‘Address’].isin([‘street_7’,‘street_4’])].head()

返回这样的行x,x[‘Address’]=s7或s4

df.loc[[True if i[-1]‘4’ or i[-1]‘7’ else False for i in df[‘Address’].values]].head()

df[‘Address’].values 返回的是array

for i in array, 则i是array里的每个值

i[-1]则是值i的最后一位

True if i[-1]==‘4’ or ‘7’, else False如果i最后一位是7或者4,则为True,反之为false,True就会被return

又因为是返回多行,所以两个[] []

本质上说,loc中能传入的只有布尔列表和索引子集构成的列表
iloc方法

通过行和列标访问

(注意与loc不同,切片右端点不包含)

单行索引
df.iloc[3]

返回第四行
如果index为数字,df.loc=df.iloc(仅单行和单列时

df.iloc[3:5]

返回3 4行,不包括右端点

单列索引
df.iloc[:,3].head()

返回第四列,这里还是没有区别

多列索引
df.iloc[:,7::-2].head()

从第8列开始倒着输出到第一,步长为2

混合索引
df.iloc[3::4,7::-2].head()

从第4行到最后,隔3行输出从第8列到第1列,每隔1列输出

函数式索引
df.iloc[lambda x:[3]].head()

为[3]的x传入df,df[3]

由上所述,iloc中接收的参数只能为整数或整数列表,不能使用布尔索引
[]操作符

如果不想陷入困境,请不要在行索引为浮点时使用[]操作符
因为在Series中的浮点[]并不是进行位置比较,而是值比较,非常特殊

Series的[]操作
单元素索引

####### s = pd.Series(df[‘Math’],index=df.index)
s[1101]
#使用的是索引标签
返回Series的index为1101的值

多行索引

####### s[0:4]
#使用的是绝对位置的整数切片,与元素无关,这里容易混淆
Series的第1-4行

函数索引

####### s[lambda x: x.index[16::-6]]
#注意使用lambda函数时,直接切片(如:s[lambda x: 16::-6])就报错
此时使用的不是绝对位置切片,而是元素切片,非常易错
有 Series x,使得x.index从17个到第一个为止,每隔5个index输出一次。将这样的Series传入s

布尔索引

####### s[s>80]
返回新的Series,其中包含所有值都是s中大于80的值

DataFrame的[]操作
单行索引

####### df[1:2]
f[]和df.loc,df.iloc一样,默认的index都是行索引
因此此处返回的是df的第二行

####### row = df.index.get_loc(1102)
df[row:row+1]
如果想要获得某一个元素,可用get_loc方法

多行索引

####### df[3:5]
用切片,如果是选取指定的某几行,推荐使用loc,否则很可能报错

单列索引

####### df[‘School’].head()

多列索引

####### df[[‘School’,‘Math’]].head()
和df.loc的多行索引一样,都需要加俩[][]

函数式索引

####### df[lambda x:[‘Math’,‘Physics’]].head()

布尔索引

####### df[df[‘Gender’]==‘F’].head()

一般来说,[]操作符常用于列选择或布尔选择,尽量避免行的选择

布尔索引

布尔符号:& - and, | - or, ~ - not 取反
df[(df[‘Gender’]‘F’)&(df[‘Address’]‘street_2’)].head()
df[(df[‘Math’]>85)|(df[‘Address’]==‘street_7’)].head()
df[~((df[‘Math’]>75)|(df[‘Address’]==‘street_1’))].head()

Math小于等于75并且Address不等于s1

df.loc[df[‘Math’]>60,(df[:8][‘Address’]==‘street_6’).values].head()#

如果不加values就会索引对齐发生错误,Pandas中的索引对齐是一个重要特征,很多时候非常使用

但是若不加以留意,就会埋下隐患

df.loc[]中有两个值,第一个为布尔列表,第二个为列表
布尔列表:Math>60,或者
前9行里Address=s1的array

isin方法
df[df[‘Address’].isin([‘street_1’,‘street_4’])&df[‘Physics’].isin([‘A’,‘A+’])]

如果只有内行df,则返回的类型是Series,df[Series]>>>DataFrame

#上面也可以用字典方式写:

df[df[[‘Address’,‘Physics’]].isin({‘Address’:[‘street_1’,‘street_4’],‘Physics’:[‘A’,‘A+’]}).all(1)]
#all与&的思路是类似的,其中的1代表按照跨列方向判断是否全为True

快速标量索引

当只需要取一个元素时,at和iat方法能够提供更快的实现
display(df.at[1101,‘School’])

display(df.loc[1101,‘School’])
display(df.iat[0,0])
display(df.iloc[0,0])

区间索引

利用interval_range方法
pd.interval_range(start=0,end=5)

#closed参数可选’left’‘right’‘both’‘neither’,默认左开右闭

pd.interval_range(start=0,periods=8,freq=5)

#periods参数控制区间个数,freq控制步长

利用cut将数值列转为区间为元素的分类变量

例如统计数学成绩的区间情况

math_interval = pd.cut(df[‘Math’],bins=[0,40,60,80,100])

#注意,如果没有类型转换,此时并不是区间类型,而是category类型
math_interval.head()

区间索引的选取
df_i = df.join(math_interval,rsuffix=’_interval’)[[‘Math’,‘Math_interval’]]\
        .reset_index().set_index('Math_interval')

df.join(新加Series), 参数rssufix: 从右框架覆盖列中使用后缀

因为Math_interval还是来源于df[Math’]的,所以算是overlapping吧

是rsuffix而不是lsuffix说明新加的Series在左而df在右

df[[’’,’’]]选取了df的这两列展示

reset_index()det_index(’ ') 把这个新的df的其中一列(暂时)设置成index,对原有df不产生影响

/.另起一行,是为了避免一行代码写太长,没有其他影响

df_i.loc[65].head()重新设置好index之后,用65进行新的index查找,会返回所有区间含有65这个数字的行
df_i.loc[[65,90]].head()

区间含有65或者90的行

GLPSOL--GLPK LP/MIP Solver 5.0 Parameter(s) specified in the command line: --write /tmp/tmpw7ceuj5f.glpk.raw --wglp /tmp/tmpaf728_06.glpk.glp --cpxlp /tmp/tmp39sanlai.pyomo.lp Reading problem data from '/tmp/tmp39sanlai.pyomo.lp'... 37 rows, 10 columns, 109 non-zeros 240 lines were read Writing problem data to '/tmp/tmpaf728_06.glpk.glp'... 206 lines were written GLPK Simplex Optimizer 5.0 37 rows, 10 columns, 109 non-zeros Preprocessing... 33 rows, 10 columns, 105 non-zeros Scaling... A: min|aij| = 9.742e-01 max|aij| = 4.271e+00 ratio = 4.384e+00 Problem data seem to be well scaled Constructing initial basis... Size of triangular part is 33 0: obj = 0.000000000e+00 inf = 2.653e+00 (2) 12: obj = 2.777882504e-02 inf = 0.000e+00 (0) * 20: obj = 2.230065746e-02 inf = 0.000e+00 (0) OPTIMAL LP SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (74879 bytes) Writing basic solution to '/tmp/tmpw7ceuj5f.glpk.raw'... 56 lines were written /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() GLPSOL--GLPK LP/MIP Solver 5.0 Parameter(s) specified in the command line: --write /tmp/tmp4qpvqduz.glpk.raw --wglp /tmp/tmpk9br_865.glpk.glp --cpxlp /tmp/tmpz6otz866.pyomo.lp Reading problem data from '/tmp/tmpz6otz866.pyomo.lp'... 37 rows, 10 columns, 109 non-zeros 240 lines were read Writing problem data to '/tmp/tmpk9br_865.glpk.glp'... 206 lines were written GLPK Simplex Optimizer 5.0 37 rows, 10 columns, 109 non-zeros Preprocessing... 33 rows, 10 columns, 105 non-zeros Scaling... A: min|aij| = 1.000e+00 max|aij| = 4.049e+00 ratio = 4.049e+00 Problem data seem to be well scaled Constructing initial basis... Size of triangular part is 33 0: obj = 0.000000000e+00 inf = 2.939e+00 (2) 12: obj = 3.156193240e-02 inf = 0.000e+00 (0) * 18: obj = 1.909919947e-02 inf = 0.000e+00 (0) OPTIMAL LP SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (74879 bytes) Writing basic solution to '/tmp/tmp4qpvqduz.glpk.raw'... 56 lines were written /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]() /root/Module_3/pack_v2.4/analysis/../lib/Weight/bwm/bwm.py:60: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0! You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy. A typical example is when you are setting values in a column of a DataFrame, like: df["col"][row_indexer] = value Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["Weight"][i-1]=Model.W[i]()
最新发布
08-31
你已经修复了 `Number` 列的问题,但仍然存在 **`ChainedAssignmentError` 警告**,这是由于在 `bwm.py` 中使用了以下写法: ```python df["Weight"][i-1] = Model.W[i]() ``` 这种写法在 Pandas 中是 **链式赋值(Chained Assignment)**,在 Pandas 3.0 即将启用的 **Copy-on-Write(CoW)机制** 下将 **完全失效**,导致赋值不生效。 --- ## ✅ 修复方式:使用 `.loc` 或 `.iloc` ### ❌ 原始写法(会触发警告): ```python df["Weight"][i-1] = Model.W[i]() ``` ### ✅ 推荐写法(使用 `.loc` 或 `.iloc`): ```python df.loc[i-1, "Weight"] = Model.W[i]() ``` 或者如果你使用的是整数索引: ```python df.iloc[i-1, df.columns.get_loc("Weight")] = Model.W[i]() ``` --- ## ✅ 示例:完整修复 `bwm.py` 中的赋值部分 ```python import pandas as pd # 假设 Model.W[i] 返回一个数值 for i in range(1, 10): # 示例循环 # ❌ df["Weight"][i-1] = Model.W[i]() # 链式赋值错误 # ✅ 正确写法: df.loc[i-1, "Weight"] = Model.W[i]() ``` --- ## 🧪 为什么链式赋值会出问题? Pandas 的 DataFrame 在某些情况下会返回 **视图(view)** 而不是副本(copy),当你使用: ```python df["col"][index] = value ``` 这实际上等价于: ```python df.__getitem__("col").__setitem__(index, value) ``` 也就是说,你是在一个中间对象上进行赋值,**Pandas 无法保证这个操作会影响原始 DataFrame**。 --- ## ✅ 修复后的好处 | 好处 | 说明 | |------|------| | ✅ 避免 `ChainedAssignmentError` | 使用 `.loc` 可以明确指定操作对象 | | ✅ 提高代码可读性 | `.loc[row, col]` 更清晰地表达意图 | | ✅ 适配 Pandas 3.0 | Copy-on-Write 模式下链式赋值将完全失效 | | ✅ 避免数据丢失或赋值不生效 | 确保赋值操作作用于原始 DataFrame | --- ## ✅
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值