目的: 做一个很简单的功能,计算显示hit列数字之和,即80=20+20+25+15.
| datetime | host | hit | volume | |
| 0 | 2018/7/24 | weibo.com | 20 | 1020 |
| 1 | 2018/7/25 | qq.com | 20 | 1028 |
| 2 | 2018/7/26 | sina.com | 25 | 1181 |
| 3 | 2018/7/27 | sohu.com | 15 | 4582 |
解决方法: Step1: 新建一个test.txt, 将下面内容复制到test.txt,然后重命名为test.csv.
,datetime,host,hit,volume
0,2018/7/24,weibo.com,20,1020
1,2018/7/25,qq.com,20,1028
2,2018/7/26,sina.com,25,1181
3,2018/7/27,sohu.com,15,4582
,,,,
双击打开test.csv,即可看到如下效果: Step2:PyCharm 代码环境输入如下命令:
import pandas as pd
CSV_FILE_PATH = './test.csv'
df = pd.read_csv(CSV_FILE_PATH)
print(df['hit'][0] + df['hit'][1]+df['hit'][2] + df['hit'][3])
Step3: 运行PyCharm,结果如下:

By the way, thanks to :
试验2:获取Score中最大值和最小值的纵坐标指引.

脚本:
import pandas as pd CSV_FILE_PATH = './zz.csv' df = pd.read_csv(CSV_FILE_PATH) idx_max=df.loc[:,['Score']].idxmax() print(idx_max) idx_min=df.loc[:,['Score']].idxmin() print(idx_min) 结果:

该博客介绍了如何利用Python的Pandas库读取CSV文件,并计算特定列(如'hit'列)的数字之和,同时展示了如何找到DataFrame中某一列(如'Score'列)的最大值和最小值对应的行索引。通过实例代码展示了数据处理的基本步骤。

228

被折叠的 条评论
为什么被折叠?



