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

相关推荐
zzc92121 小时前
Tensorflow 2.X Debug中的Tensor.numpy问题 @tf.function
人工智能·tensorflow·numpy
liuweidong08021 天前
【Pandas】pandas DataFrame mode
python·数据挖掘·pandas
xiaohanbao092 天前
day29 python深入探索类装饰器
开发语言·python·学习·机器学习·pandas
正在走向自律3 天前
Conda 完全指南:从环境管理到工具集成
开发语言·python·conda·numpy·fastapi·pip·开发工具
xiaohanbao095 天前
day26 Python 自定义函数
开发语言·python·学习·机器学习·信息可视化·numpy
老哥不老6 天前
Python调用SQLite及pandas相关API详解
python·sqlite·pandas
来自星星的坤7 天前
深入理解 NumPy:Python 科学计算的基石
开发语言·python·numpy
Hello world.Joey7 天前
数据挖掘入门-二手车交易价格预测
人工智能·python·数据挖掘·数据分析·conda·pandas
liuweidong08027 天前
【Pandas】pandas DataFrame cumprod
pandas
wxl7812279 天前
基于flask+pandas+csv的报表实现
python·flask·pandas