栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用iloc为pandas DataFrame中的特定单元格设置值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用iloc为pandas DataFrame中的特定单元格设置值

对于混合位置和索引,请使用

.ix
。但是您需要确保索引不是整数,否则会引起混乱。

df.ix[0, 'COL_NAME'] = x

更新:

或者,尝试

df.iloc[0, df.columns.get_loc('COL_NAME')] = x

例:

import pandas as pdimport numpy as np# your data# ========================np.random.seed(0)df = pd.Dataframe(np.random.randn(10, 2), columns=['col1', 'col2'], index=np.random.randint(1,100,10)).sort_index()print(df)      col1    col210  1.7641  0.400224  0.1440  1.454329  0.3131 -0.854132  0.9501 -0.151433  1.8676 -0.977336  0.7610  0.121756  1.4941 -0.205258  0.9787  2.240975 -0.1032  0.410676  0.4439  0.3337# .iloc with get_loc# ===================================df.iloc[0, df.columns.get_loc('col2')] = 100df      col1      col210  1.7641  100.000024  0.1440    1.454329  0.3131   -0.854132  0.9501   -0.151433  1.8676   -0.977336  0.7610    0.121756  1.4941   -0.205258  0.9787    2.240975 -0.1032    0.410676  0.4439    0.3337


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/640999.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号