python - Pandas 在同一列上导入具有索引级别的多索引 csv
问题描述
我有一个具有以下格式的多索引 csv:
; ;2000;2001;2002;2003;2004;2005;2006;2007;2008;2009;2010;2011;2012;2013;2014;2015;2016;2017
CO2;;;;;;;;;;;;;;;;;;;
010000 Agriculture and horticulture;AZZ;2312;2249;2165;2102;2034;2095;2106;2067;2060;1935;1985;1983;1893;1865;1750;1728;1777;1736
020000 Forestry;AZZ;40;42;39;43;46;50;49;49;46;52;62;62;67;60;63;66;67;66
030000 Fishing;AZZ;785;767;746;722;645;655;629;580;501;485;472;441;351;384;352;382;387;377
; ;2000;2001;2002;2003;2004;2005;2006;2007;2008;2009;2010;2011;2012;2013;2014;2015;2016;2017
More CO2;;;;;;;;;;;;;;;;;;;
010000 Agriculture and horticulture;AZZ;2312;2249;2165;2102;2034;2095;2106;2067;2060;1935;1985;1983;1893;1865;1750;1728;1777;1736
020000 Forestry;AZZ;40;42;39;43;46;50;49;49;46;52;62;62;67;60;63;66;67;66
030000 Fishing;AZZ;785;767;746;722;645;655;629;580;501;485;472;441;351;384;352;382;387;377
所以 MultiIndex 的两个级别实际上都在同一列上。
我正在尝试按如下方式导入它:
df=pd.read_csv('my.csv',sep=";",header=[0],index_col=[0])
但这会返回以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 24: invalid start byte
我不确定位置 24 指的是哪里以及如何继续导入文件。
期望的输出
2000 2001 2002 2003 ...
CO2 010000 Agriculture and horticulture AZZ 2312.0 2249.0 2165.0 2102.0 ...
020000 Forestry AZZ 40.0 42.0 39.0 43.0 ...
030000 Fishing AZZ 785.0 767.0 746.0 722.0 ...
060000 Extraction of oil and gas BZ1 2174.0 2190.0 2184.0 2188.0 ...
080090 Extraction of gravel and stone BZ2 295.0 332.0 304.0 277.0 ...
2000 2001 2002 2003 ...
More CO2 010000 Agriculture and horticulture AZZ 2312.0 2249.0 2165.0 2102.0 ...
020000 Forestry AZZ 40.0 42.0 39.0 43.0 ...
030000 Fishing AZZ 785.0 767.0 746.0 722.0 ...
060000 Extraction of oil and gas BZ1 2174.0 2190.0 2184.0 2188.0 ...
080090 Extraction of gravel and stone BZ2 295.0 332.0 304.0 277.0 ...
解决方案
你可以编码 gbk 来阅读
df=pd.read_csv('./AirEmissions117.csv',sep=';',encoding='gbk')
推荐阅读
- python - pandas:groupby 列结果以另一列为条件
- swift - 在点击 TableViewCell Image(swift) 后,如何调用/显示用于拍摄或从 Main VC 上的图库中选择照片的操作表
- python - 如何检查硒铬窗口是否关闭python
- javascript - 将链接添加到表格 antd 中的单元格
- python - 如何使用python opencv删除文件夹中的特定图像
- python - AssertionError:新值的形状必须与经理形状兼容
- firebase - Firebase Auth中的密码是否有任何到期时间
- drupal - 如何在正确的位置设置错误消息 drupal 7?
- flutter - 如何修复 MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现)
- java - 我将 spring boot 应用程序更改为 spring mvc 但我的 log4j2.xml 不起作用?