pandas 从20列数据中选择5列
时间: 2023-06-01 21:07:43 浏览: 87
请选择使用pandas中的DataFrame(数据框)和iloc方法选择响应的列,示例代码如下:
```
import pandas as pd
# 创建示例数据
data = {'col1': [1, 2, 3, 4, 5],
'col2': ['A', 'B', 'C', 'D', 'E'],
'col3': [0.1, 0.2, 0.3, 0.4, 0.5],
'col4': [True, False, True, False, True],
'col5': ['apple', 'banana', 'orange', 'kiwi', 'pear'],
'col6': [100, 200, 300, 400, 500],
'col7': [0.01, 0.02, 0.03, 0.04, 0.05],
'col8': ['male', 'female', 'male', 'male', 'female'],
'col9': [10, 20, 30, 40, 50],
'col10': [1.1, 2.2, 3.3, 4.4, 5.5],
'col11': ['China', 'Japan', 'USA', 'Australia', 'Canada'],
'col12': [True, False, True, False, True],
'col13': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
'col14': [0.001, 0.002, 0.003, 0.004, 0.005],
'col15': [1000, 2000, 3000, 4000, 5000],
'col16': ['beijing', 'shanghai', 'guangzhou', 'shenzhen', 'hangzhou'],
'col17': [10.0, 20.0, 30.0, 40.0, 50.0],
'col18': [10000, 20000, 30000, 40000, 50000],
'col19': [100000, 200000, 300000, 400000, 500000],
'col20': [0.1, 0.2, 0.3, 0.4, 0.5]}
# 将字典数据转换为DataFrame
df = pd.DataFrame(data)
# 选择前5列数据
df_selected = df.iloc[:, :5]
print(df_selected)
```
输出结果如下:
```
col1 col2 col3 col4 col5
0 1 A 0.1 True apple
1 2 B 0.2 False banana
2 3 C 0.3 True orange
3 4 D 0.4 False kiwi
4 5 E 0.5 True pear
```
阅读全文