import pandas as pd data={'sum':[1,2,3,4,5], 'mon':[98,87,76,54,53], 'tur':[12,24,36,48,60], 'wed':[22,33,11,44,66], 'thu':[1,5,4,3,2], 'fri':[5,6,7,8,9], 'sat':[15,63,45,65,78] } df=pd.DataFrame(data,index=[10,20,30,40,50]) print(df) print("输出wed列:") print(df[1:2]) print("输出第2、3、4行的mon、tue列的单元格:") print(df.loc['20':'40',['mon','tue']])
时间: 2023-07-02 15:10:12 浏览: 38
你的代码中有一个小错误,应该是 'tur':[12,24,36,48,60] 而不是 'tue':[12,24,36,48,60]。修改后的代码如下:
```python
import pandas as pd
data = {'sum': [1, 2, 3, 4, 5],
'mon': [98, 87, 76, 54, 53],
'tur': [12, 24, 36, 48, 60],
'wed': [22, 33, 11, 44, 66],
'thu': [1, 5, 4, 3, 2],
'fri': [5, 6, 7, 8, 9],
'sat': [15, 63, 45, 65, 78]
}
df = pd.DataFrame(data, index=[10, 20, 30, 40, 50])
print(df)
print("输出 wed 列:")
print(df['wed'])
print("输出第2、3、4行的 mon、tur 列的单元格:")
print(df.loc[20:40, ['mon', 'tur']])
```
输出结果如下:
```
sum mon tur wed thu fri sat
10 1 98 12 22 1 5 15
20 2 87 24 33 5 6 63
30 3 76 36 11 4 7 45
40 4 54 48 44 3 8 65
50 5 53 60 66 2 9 78
输出 wed 列:
10 22
20 33
30 11
40 44
50 66
Name: wed, dtype: int64
输出第2、3、4行的 mon、tur 列的单元格:
mon tur
20 87 24
30 76 36
40 54 48
```