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

Python 中读取一列数据
使用 Python 读取 DataFrame 中一列数据的常见方法包括:
1. 使用 .loc[]
<code class="python">df['column_name']</code>
其中 column_name 是要读取的列的名称。
立即学习“Python免费学习笔记(深入)”;
2. 使用 .iloc[]
<code class="python">df.iloc[:, column_index]</code>
其中 column_index 是要读取的列的索引(从 0 开始)。
3. 使用 .at[]
<code class="python">df.at[row_index, 'column_name']</code>
其中 row_index 是要读取的行的索引,column_name 是要读取的列的名称。
4. 使用 .iat[]
<code class="python">df.iat[row_index, column_index]</code>
其中 row_index 是要读取的行的索引,column_index 是要读取的列的索引(从 0 开始)。
示例:
读取 DataFrame 中名为 "Name" 的一列:
<code class="python">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]</code>











