卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章55706本站已运行404

python怎么读取一列数据

python 中读取一列数据的方法有四种:1. .loc[];2. .iloc[];3. .at[];4. .iat[]。

python怎么读取一列数据

Python 中读取一列数据

使用 Python 读取 DataFrame 中一列数据的常见方法包括:

1. 使用 .loc[]

df['column_name']

其中 column_name 是要读取的列的名称。

2. 使用 .iloc[]

df.iloc[:, column_index]

其中 column_index 是要读取的列的索引(从 0 开始)。

3. 使用 .at[]

df.at[row_index, 'column_name']

其中 row_index 是要读取的行的索引,column_name 是要读取的列的名称。

4. 使用 .iat[]

df.iat[row_index, column_index]

其中 row_index 是要读取的行的索引,column_index 是要读取的列的索引(从 0 开始)。

示例:

读取 DataFrame 中名为 "Name" 的一列:

import pandas as pd

df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [20, 25, 30]})

# 使用 .loc[]
names = df['Name']

# 使用 .iloc[]
names = df.iloc[:, 0]

# 使用 .at[]
first_name = df.at[0, 'Name']

# 使用 .iat[]
second_name = df.iat[1, 0]
卓越飞翔博客
上一篇: python怎么一键加井号
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏