我读过一些关于 Python2对 Pandas 的 to _ csv (... ... 等等... ...)的限制的文章。我也遇到了吗?我使用的是 Python 2.7.3
当字符串中包含 - 和 ≥ 号时会出现乱码。除此之外,导出没问题。
df.to_csv("file.csv", encoding="utf-8")
有什么解决办法吗?
df.head() 是这样的:
demography Adults ≥49 yrs Adults 18−49 yrs at high risk|| \
state
Alabama 32.7 38.6
Alaska 31.2 33.2
Arizona 22.9 38.8
Arkansas 31.2 34.0
California 29.8 38.8
CSV 输出是这样的
state, Adults ≥49 yrs, Adults 18−49 yrs at high risk||
0, Alabama, 32.7, 38.6
1, Alaska, 31.2, 33.2
2, Arizona, 22.9, 38.8
3, Arkansas,31.2, 34
4, California,29.8, 38.8
完整代码是这样的:
import pandas
import xlrd
import csv
import json
df = pandas.DataFrame()
dy = pandas.DataFrame()
# first merge all this xls together
workbook = xlrd.open_workbook('csv_merger/vaccoverage.xls')
worksheets = workbook.sheet_names()
for i in range(3,len(worksheets)):
dy = pandas.io.excel.read_excel(workbook, i, engine='xlrd', index=None)
i = i+1
df = df.append(dy)
df.index.name = "index"
df.columns = ['demography', 'area','state', 'month', 'rate', 'moe']
#Then just grab month = 'May'
may_mask = df['month'] == "May"
may_df = (df[may_mask])
#then delete some columns we dont need
may_df = may_df.drop('area', 1)
may_df = may_df.drop('month', 1)
may_df = may_df.drop('moe', 1)
print may_df.dtypes #uh oh, it sees 'rate' as type 'object', not 'float'. Better change that.
may_df = may_df.convert_objects('rate', convert_numeric=True)
print may_df.dtypes #that's better
res = may_df.pivot_table('rate', 'state', 'demography')
print res.head()
#and this is going to spit out an array of Objects, each Object a state containing its demographics
res.reset_index().to_json("thejson.json", orient='records')
#and a .csv for good measure
res.reset_index().to_csv("thecsv.csv", orient='records', encoding="utf-8")