Python读写csv文件
1、引入所需要的包
import pandas as pd
import numpy as np
2、用pandas读取
data = pd.read_table("G:\城市_20140513-20141231\china_cities_20140513.csv",sep=",")
print (data)
运行结果
date hour ype 北京 天津 石家庄 唐山 秦皇岛
0 20140513 0 AQI 81.00 69.00 125.00 82.00 66.00
1 20140513 0 PM2.5 49.00 44.00 66.00 55.00 24.00
2 20140513 0 PM2.5_24h 35.00 43.00 39.00 40.00 22.00
3 20140513 0 PM10 112.00 87.00 199.00 113.00 82.00
4 20140513 0 PM10_24h 73.00 76.00 112.00 80.00 57.00
.. ... ... ... ... ... ... ... ...
340 20140513 23 O3_24h 129.00 167.00 119.00 215.00 105.00
341 20140513 23 O3_8h 98.00 90.00 51.00 158.00 65.00
342 20140513 23 O3_8h_24h 114.00 122.00 97.00 189.00 90.00
343 20140513 23 CO 0.23 1.10 1.54 0.89 1.75
344 20140513 23 CO_24h 0.64 1.21 1.12 0.97 1.80
3、写入新的csv文件
#选出"date","hour","type","唐山"列数据
data_tangshan=data[["date","hour","type","唐山"]]
print(data_tangshan)
#将data_tangshan保存成新的csv文件
data_tangshan.to_csv("new.csv",index=False,sep=',')
运行结果
date hour type 唐山
0 20140513 0 AQI 82.00
1 20140513 0 PM2.5 55.00
2 20140513 0 PM2.5_24h 40.00
3 20140513 0 PM10 113.00
4 20140513 0 PM10_24h 80.00
.. ... ... ... ...
340 20140513 23 O3_24h 215.00
341 20140513 23 O3_8h 158.00
342 20140513 23 O3_8h_24h 189.00
343 20140513 23 CO 0.89
344 20140513 23 CO_24h 0.97