在Python中,提取列表或数据框(DataFrame)的一列可以通过以下方法实现:
列表提取一列
使用列表解析
```python
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column = [row for row in lst]
print(column) 输出 [2, 5, 8]
使用循环
```python
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
column = []
for row in lst:
column.append(row)
print(column) 输出 [2, 5, 8]
Pandas DataFrame提取一列
使用列名
```python
import pandas as pd
dfd = {11: ['a', 'b', 'c'], 22: ['d', 'e', 'f'], 33: ['g', 'h', 'i']}
df = pd.DataFrame(dfd)
df_11 = df
print(df_11) 输出 0a
1b
2c
Name: 11, dtype: object
使用列号(iloc方法)
```python
df = pd.DataFrame(dfd)
使用iloc按位置取列,位置从0开始
df_11 = df.iloc[:, 1]
print(df_11) 输出 0a
1b
2c
Name: 11, dtype: object
以上是提取列表或Pandas DataFrame一列的基本方法。请根据您的具体需求选择合适的方法