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

相关推荐
七夜zippoe16 小时前
NumPy高级:结构化数组与内存布局优化实战指南
python·架构·numpy·内存·视图
世界尽头与你2 天前
MySQL索引入门
数据库·mysql·索引
waves浪游3 天前
Ext系列文件系统
linux·服务器·开发语言·c++·numpy
强化试剂瓶4 天前
Silane-PEG8-DBCO,硅烷-聚乙二醇8-二苯并环辛炔技术应用全解析
python·flask·numpy·pyqt·fastapi
qq_381454994 天前
Python Pandas完全指南:从核心数据结构到实战操作
pandas
TUTO_TUTO4 天前
【python-词汇指标提取工具开发】自学笔记(1)-后端程序
人工智能·python·pandas·visual studio code
葱明撅腚5 天前
shapely空间数据分析
python·pandas·gis·shapely
Python-AI Xenon5 天前
RHEL / CentOs 7.9 离线升级OpenSSH完整指南
linux·centos·numpy
忘忧记5 天前
pandas基础三
pandas
和小胖11225 天前
Anaconda虚拟环境创建步骤
python·conda·numpy