目的:
做一个很简单的功能,计算显示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 :
pandas读取csv文件数据的方法及注意点 - 简书
|