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

相关推荐
人工干智能12 小时前
科普:Pandas 索引器 loc 与 iloc 的编程思维
java·人工智能·pandas
梅雅达编程笔记12 小时前
编程启蒙|Scratch 转 Python 系列第9天:字典/哈希表积木双向对照(AI大模型参数配置表实战)
开发语言·人工智能·python·numpy·pandas
benchmark_cc1 天前
如何用 Python + QuantDash 快速构建高胜率“配对交易(Pairs Trading)”策略?
开发语言·人工智能·python·pandas·量化交易·quantdash
汤姆小白3 天前
08-应用部署
人工智能·python·机器学习·numpy·transformer
汤姆小白4 天前
02-Tokenizer原理与实现
人工智能·python·机器学习·numpy
weixin_307779134 天前
Python Pandas读取Excel Sheet生成SQL WHERE条件
python·sql·自动化·excel·pandas
汤姆小白4 天前
01-环境搭建与项目导览
人工智能·python·机器学习·numpy
CS创新实验室5 天前
NumPy数组的C风格和Fortran风格
c语言·开发语言·numpy
梅雅达编程笔记5 天前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
你想知道什么?9 天前
Pandas学习笔记
笔记·学习·pandas