pandas.loc()实践

本文深入探讨了Pandas中loc函数的高级应用,包括如何通过条件表达式和布尔索引进行数据筛选,以及如何更新DataFrame中的特定单元格。通过实例展示了loc与条件表达式的结合使用,为数据分析师提供了强大的数据处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

loc只能通过index和列名来用!!!
iloc可以通过类似于list的索引(位置)来取
ix可以通过混合提取

df.loc[[1,1,1,4,0,0], 'sign']=1
print(df)
 id       date       city category  age   price  sign

0 1001 2013-01-02 Beijing 100-A 23 1200.0 1.0
1 1002 2013-01-03 SH 100-B 44 NaN 1.0
2 1003 2013-01-04 guangzhou 110-A 54 2133.0 NaN
3 1004 2013-01-05 Shenzhen 110-C 32 5433.0 NaN
4 1005 2013-01-06 shanghai 210-A 34 NaN 1.0
5 1006 2013-01-07 Beijing 130-F 32 4432.0 NaN

df.loc[(df['city'] == 'Beijing') & (df['price'] >= 0), 'sign']=1
print(df)
df.loc[[True,True,True,True,False,True], 'sign']=1
print(df)
 id       date       city category  age   price  sign

0 1001 2013-01-02 Beijing 100-A 23 1200.0 1.0
1 1002 2013-01-03 SH 100-B 44 NaN 1.0
2 1003 2013-01-04 guangzhou 110-A 54 2133.0 1.0
3 1004 2013-01-05 Shenzhen 110-C 32 5433.0 1.0
4 1005 2013-01-06 shanghai 210-A 34 NaN NaN
5 1006 2013-01-07 Beijing 130-F 32 4432.0 1.0
可以通过布尔值或者index或者切片来取,如果为布尔值那么就必须等长,为True行的被取到,index和切片同理。

<ipython-input-7-ab05c5cfb290>:825: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].apply( <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:830: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna("").astype(str) <ipython-input-7-ab05c5cfb290>:830: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna("").astype(str) <ipython-input-7-ab05c5cfb290>:830: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna("").astype(str) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:833: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["未匹配原因"] = "日期或金额不符" <ipython-input-7-ab05c5cfb290>:825: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].apply( <ipython-input-7-ab05c5cfb290>:830: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna("").astype(str) <ipython-input-7-ab05c5cfb290>:830: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna("").astype(str) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:819: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df[col] = df[col].fillna(0).astype(float).round(2) <ipython-input-7-ab05c5cfb290>:833: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df["未匹配原因"] = "日期或金额不符" 以上这是什么提示?
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值