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

相关推荐
wudl556614 小时前
NumPy -数组属性与形状操作
numpy
njxiejing1 天前
Numpy一维、二维、三维数组切片实例
开发语言·python·numpy
wudl55661 天前
Pandas-之数据可视化
信息可视化·数据分析·pandas
wudl55661 天前
NumPy 简介与安装
numpy
wudl55662 天前
NumPy -数组运算与操作
numpy
万粉变现经纪人2 天前
如何解决 pip install 安装报错 [WinError 32] 文件被占用(杀毒/占用进程)问题
python·pycharm·flask·beautifulsoup·bug·pandas·pip
云和数据.ChenGuang2 天前
SyntaxError: Non-UTF-8 code starting
人工智能·python·numpy
文人sec2 天前
使用python-pandas-openpyxl编写运营查询小工具
开发语言·python·pandas
咋吃都不胖lyh2 天前
比较两个excel文件的指定列是否一致
爬虫·python·pandas
IT小哥哥呀4 天前
Python实用技巧:批量处理Excel数据并生成销售报表(含实战案例)
python·pandas·数据可视化·数据处理·报表生成·excel自动化·办公神器