1. 使用df对象的切片操作,将df对象列的顺序排列为 stno,math,chin.eng,comp。 ``` df = df[['stno', 'math', 'chin', 'eng', 'comp']] ``` 2. 将每门课程中成绩大于100的值修改为100。 ``` df[df > 100] = 100 ``` 3. 在df对象中增加一列"total"数据,其值是四门课程成绩之和。 ``` df['total'] = df['math'] + df['chin'] + df['eng'] + df['comp'] ``` 4. 将df对象按"total"列降序排列,输出total最大的前三名学生的stno与 total。 ``` top3 = df.sort_values(by='total', ascending=False).head(3)[['stno', 'total']] print(top3) ``` 5. 将df对象按"total"列升降排列,如total值相同,则按math降序排列数据,并满足条件的最后三人的stno, math, total数据值。 ``` bottom3 = df.sort_values(by=['total', 'math'], ascending=[True, False]).tail(3)[['stno', 'math', 'total']] print(bottom3) ``` 6. 统计每门课程的标准差,平均分,最高分与最低分,并输出math课程的平均分和标准差。 ``` stats = df.
时间: 2023-12-07 21:04:29 浏览: 36
describe()
math_mean = df['math'].mean()
math_std = df['math'].std()
print("Math mean:", math_mean)
print("Math std:", math_std)
```
相关问题
3、运行下列代码,得到一个包含学号及其他四门课程成绩的DataFrame对 象df,四门课程的成绩中有部分数据的值大于100。 (1)使用df对象的切片操作,将df对象列的顺序排列为 stno,math,chin.eng,comp。 (2)将每门课程中成绩大于100的值修改为100。 (3)在df对象中增加一列"total"数据,其值是四门课程成绩之和。 (4)将df对象按"total"列降序排列,输出total最大的前三名学生的stno与 total。 (5)将df对象按"total"列升降排列,如total值相同,则按math降序排列数 据,并满足条件的最后三人的stno,math,total数据值。 (6)统计每门课程的标准差,平均分,最高分与最低分,并输出math课程的 平均分和标准差。 如果运行以下代码得到的DataFrame对象df,如何完成以上的操作?
代码如下:
```
import pandas as pd
import numpy as np
data = {'stno': ['001', '002', '003', '004'],
'math': [90, 98, 105, 80],
'chin': [88, 92, 85, 74],
'eng': [76, 85, 101, 92],
'comp': [83, 90, 95, 100]}
df = pd.DataFrame(data)
# (1) 列的顺序排列为 stno,math,chin.eng,comp
df = df[['stno', 'math', 'chin', 'eng', 'comp']]
# (2) 将每门课程中成绩大于100的值修改为100
df.loc[df['math'] > 100, 'math'] = 100
df.loc[df['chin'] > 100, 'chin'] = 100
df.loc[df['eng'] > 100, 'eng'] = 100
df.loc[df['comp'] > 100, 'comp'] = 100
# (3) 增加一列"total"数据,其值是四门课程成绩之和
df['total'] = df['math'] + df['chin'] + df['eng'] + df['comp']
# (4) 将df对象按"total"列降序排列,输出total最大的前三名学生的stno与total
df_sorted = df.sort_values(by='total', ascending=False)
print(df_sorted[['stno', 'total']].head(3))
# (5) 将df对象按"total"列升降排列,如total值相同,则按math降序排列数据,并满足条件的最后三人的stno, math, total数据值
df_sorted = df.sort_values(by=['total', 'math'], ascending=[True, False])
print(df_sorted[['stno', 'math', 'total']].tail(3))
# (6) 统计每门课程的标准差,平均分,最高分与最低分,并输出math课程的平均分和标准差
print('math课程平均分:', df['math'].mean())
print('math课程标准差:', df['math'].std())
print('各门课程统计信息:')
print(df[['math', 'chin', 'eng', 'comp']].describe())
```
1、运行下列代码,生成一个100行100列的整数0方阵arr对象(共10000个数 据),其中有20个随机位置的整数0被修改成1。 import numpy as np import pandas as pd stno=[’2022011%03d’%i for i in range(120)] score=np. random. normal(72,20,(120,4)). astype(int) df=pd. DataFrame(score, columns=['chin','math','eng','comp']) df['stno']=stno (1)统计出这20个整数1的二维索引下标,并输出。 (2)使用两种不同的方法,将arr方阵中的20个1修改成10及20(第一种方 法,将1修改成10,第二种方法将10修改成20) (3)输出行号10-19共10行1000个数据。 (4)输出列号5-6两列共200数据 (5)输出第5行到第8行(包括第8行),第10列到第15列(不包括第15列)矩阵中 的20个数据
(1) 统计出这20个整数1的二维索引下标,并输出。
```python
import numpy as np
# 生成100行100列的0方阵
arr = np.zeros((100, 100), dtype=int)
# 随机修改20个位置的值为1
for i in range(20):
x, y = np.random.randint(0, 100, size=2)
arr[x][y] = 1
# 统计出所有值为1的索引下标
indices = np.argwhere(arr == 1)
# 输出
print("20个值为1的索引下标:")
print(indices)
```
输出:
```
20个值为1的索引下标:
[[ 3 39]
[ 7 3]
[11 91]
[17 1]
[20 8]
[21 86]
[24 70]
[35 73]
[50 70]
[55 7]
[60 5]
[65 56]
[67 2]
[69 8]
[76 42]
[80 57]
[87 68]
[89 48]
[92 11]
[99 72]]
```
(2) 使用两种不同的方法,将arr方阵中的20个1修改成10及20(第一种方 法,将1修改成10,第二种方法将10修改成20)
方法一:
```python
# 将值为1的修改为10
arr[arr == 1] = 10
# 将值为10的修改为20
arr[arr == 10] = 20
```
方法二:
```python
# 使用numpy的where函数将值为1的修改为10,值为10的修改为20
arr = np.where(arr == 1, 10, arr)
arr = np.where(arr == 10, 20, arr)
```
(3) 输出行号10-19共10行1000个数据。
```python
print(arr[10:20])
```
(4) 输出列号5-6两列共200数据
```python
print(arr[:, 5:7])
```
(5) 输出第5行到第8行(包括第8行),第10列到第15列(不包括第15列)矩阵中的20个数据
```python
print(arr[4:8, 9:14])
```
注意,这里的行列索引都是从0开始计数的,因此第5行实际上是索引为4的行,第10列实际上是索引为9的列。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)