[‘column‘]和[:,‘column‘]的区别

之前,关于numpy和pandas的操作一直不熟悉,对于获取数据中的行,列一直混淆。

df['column']

df'column'是 Pandas DataFrame 切片的常用语法,用于选择名为 'column' 的单个列。它返回一个 Pandas Series 对象。

df.loc[:,'column']

df[:, 'popularity'] 这种语法是不正确的,Pandas 不支持这种索引方式。

``[:, 'popularity'] 这种切片语法通常用于 NumPy 数组,表示选择所有行的指定列。

如果试图在Pandas DataFrame 中使用多维切片,可以使用 lociloc 方法

使用 loc 按标签索引

正确的多维切片方法

popularity_series = train.loc:, 'popularity'

使用 iloc 按位置索引

iloc 方法主要用于基于位置的索引(整数位置索引),而不是标签。

假设 'popularity' 列是 DataFrame 中的第 0 列

popularity_series = train.iloc:, 0

相关推荐
m沐沐11 天前
【计算机视觉】OpenCV 模板匹配银行卡数字识别---下
人工智能·python·opencv·计算机视觉·pycharm·numpy
李昊哲小课12 天前
PyArrow 完整教程
大数据·数据分析·pandas·pyarrow
云和数据.ChenGuang12 天前
T5大模型
人工智能·机器人·pandas·数据预处理·数据训练
云和数据.ChenGuang13 天前
大模型厂商常用的数据库有哪些?
数据库·人工智能·pytorch·深度学习·numpy
MATLAB代码顾问14 天前
Python Pandas数据分析入门指南
python·数据分析·pandas
MATLAB代码顾问14 天前
Python NumPy数值计算核心指南
开发语言·python·numpy
FBI HackerHarry浩14 天前
解决pip 安装 numpy 时元数据生成失败
numpy·pip
代码讲故事15 天前
在没有kibana的ES(elasticsearch)线上生产环境集群中,如何通过命令行修改或增加字段而不需要reindex?
大数据·elasticsearch·搜索引擎·命令行·es·索引·模版
themingyi16 天前
Abaqus2024安装python包pandas
开发语言·python·pandas
一晌小贪欢16 天前
第26节:自动化办公——利用 Python 自动生成动态分析报告 (PPT/PDF)
开发语言·python·数据分析·自动化·powerpoint·pandas·数据可视化