[‘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]

相关推荐
萧曵 丶6 小时前
MySQL 主键不推荐使用 UUID 的深层原因
数据库·mysql·索引
橙露17 小时前
从零基础到实战:Python 数据分析三剑客(Pandas+NumPy+Matplotlib)核心应用指南
python·数据分析·pandas
muddjsv1 天前
NumPy 常用工具:统计、排序、缺失值处理
numpy
清水白石0081 天前
手写超速 CSV 解析器:利用 multiprocessing 与 mmap 实现 10 倍 Pandas 加速
python·pandas
muddjsv2 天前
NumPy 核心运算:向量化与广播
numpy
muddjsv2 天前
NumPy 实战:从基础到场景化应用
numpy
A尘埃2 天前
Numpy常用方法介绍
numpy
belldeep3 天前
python:mnist 数据集下载,parse
python·numpy·mnist
佛祖让我来巡山3 天前
Numpy
机器学习·数据分析·numpy·矢量运算
Hello.Reader3 天前
PyFlink 向量化 UDF(Vectorized UDF)Arrow 批传输原理、pandas 标量/聚合函数、配置与内存陷阱、五种写法一网打尽
python·flink·pandas