如何将数据从csv读取到具有多列的pandas数据框中?

如何解决如何将数据从csv读取到具有多列的pandas数据框中?

  • 我正在尝试绘制日本的高度随时间变化的情况。
  • 尝试使用df['Country']选择数据时,会出现KeyError: 'Country'
  • 如何读取CSV以便有多列?
import pandas as pd

df = pd.read_csv('japan.csv')

# display(df.head(10))
  "Country","ISO","Sex","Year of birth","Mean height (cm)","Mean height lower 95% uncertainty interval (cm)","Mean height upper 95% uncertainty interval (cm)"
0                                                                                         "Japan","JPN","Men","1896","156.1669497","155.3290164","157.0193865"
1                                                                                         "Japan","1897","156.3667318","155.5748215","157.1688422"
2                                                                                         "Japan","1898","156.5671293","155.8227956","157.3239153"
3                                                                                         "Japan","1899","156.7681598","156.0657473","157.4824652"
4                                                                                          "Japan","1900","156.9695293","156.289973","157.6533546"
5                                                                                         "Japan","1901","157.1709281","156.5166868","157.8325827"
6                                                                                         "Japan","1902","157.3717481","156.7487729","158.0021031"
7                                                                                         "Japan","1903","157.5714121","156.9624115","158.1789401"
8                                                                                         "Japan","1904","157.7693278","157.1738079","158.3746806"
9                                                                                         "Japan","1905","157.9648999","157.3823179","158.5607832"

df['"Country","Mean height upper 95% uncertainty interval (cm)"'].head()  # returns the top five rows

df['Country'].head()  # results in a KeyError

错误


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
e:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,key,method,tolerance)
   2888             try:
-> 2889                 return self._engine.get_loc(casted_key)
   2890             except KeyError as err:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Country'

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-61-5913eb6be58c> in <module>
----> 1 df['Country']

e:\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self,key)
   2897             if self.columns.nlevels > 1:
   2898                 return self._getitem_multilevel(key)
-> 2899             indexer = self.columns.get_loc(key)
   2900             if is_integer(indexer):
   2901                 indexer = [indexer]

e:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,tolerance)
   2889                 return self._engine.get_loc(casted_key)
   2890             except KeyError as err:
-> 2891                 raise KeyError(key) from err
   2892 
   2893         if tolerance is not None:

KeyError: 'Country'

样本子集japan.csv

"""Country"",""ISO"",""Sex"",""Year of birth"",""Mean height (cm)"",""Mean height lower 95% uncertainty interval (cm)"",""Mean height upper 95% uncertainty interval (cm)"""
"""Japan"",""JPN"",""Men"",""1896"",""156.1669497"",""155.3290164"",""157.0193865"""
"""Japan"",""1897"",""156.3667318"",""155.5748215"",""157.1688422"""
"""Japan"",""1898"",""156.5671293"",""155.8227956"",""157.3239153"""
"""Japan"",""1899"",""156.7681598"",""156.0657473"",""157.4824652"""
"""Japan"",""1900"",""156.9695293"",""156.289973"",""157.6533546"""
"""Japan"",""1901"",""157.1709281"",""156.5166868"",""157.8325827"""
"""Japan"",""1902"",""157.3717481"",""156.7487729"",""158.0021031"""
"""Japan"",""1903"",""157.5714121"",""156.9624115"",""158.1789401"""
"""Japan"",""1904"",""157.7693278"",""157.1738079"",""158.3746806"""
"""Japan"",""1905"",""157.9648999"",""157.3823179"",""158.5607832"""
"""Japan"",""1906"",""158.1573765"",""157.5884274"",""158.742553"""
"""Japan"",""1907"",""158.3466531"",""157.7895401"",""158.915132"""
"""Japan"",""1908"",""158.5328308"",""157.9841725"",""159.0925714"""
"""Japan"",""1909"",""158.7160861"",""158.1753708"",""159.276479"""
"""Japan"",""1910"",""158.89661"",""158.370059"",""159.450577"""
"""Japan"",""1911"",""159.0763866"",""158.5628661"",""159.6237299"""
"""Japan"",""1912"",""159.2556859"",""158.7449046"",""159.7944815"""
"""Japan"",""1913"",""159.4346077"",""158.9303687"",""159.9603557"""
"""Japan"",""1914"",""159.6140561"",""159.1130665"",""160.1318441"""
"""Japan"",""1915"",""159.7950539"",""159.3024859"",""160.3064244"""
"""Japan"",""1916"",""159.9785479"",""159.4954302"",""160.4809011"""
"""Japan"",""1917"",""160.1650894"",""159.6869547"",""160.6585355"""
"""Japan"",""1918"",""160.3552175"",""159.8795709"",""160.8458981"""
"""Japan"",""1919"",""160.5471501"",""160.080955"",""161.0301727"""
"""Japan"",""1920"",""160.7402618"",""160.280457"",""161.2185888"""
"""Japan"",""1921"",""160.9324028"",""160.4819947"",""161.4019606"""
"""Japan"",""1922"",""161.1218617"",""160.6773033"",""161.5813391"""
"""Japan"",""1923"",""161.309733"",""160.872278"",""161.7682169"""
"""Japan"",""1924"",""161.4966186"",""161.066423"",""161.9402205"""
"""Japan"",""1925"",""161.6856993"",""161.2608407"",""162.1171119"""
"""Japan"",""1926"",""161.8793066"",""161.4677212"",""162.3062195"""
"""Japan"",""1927"",""162.0796283"",""161.6745845"",""162.5013398"""
"""Japan"",""1928"",""162.2876431"",""161.8873343"",""162.7016081"""
"""Japan"",""1929"",""162.5028113"",""162.1097493"",""162.9121854"""

来自https://jsfiddle.net/ukd5stc0/的原始数据示例

Country,ISO,Sex,Year of birth,Mean height (cm),Mean height lower 95% uncertainty interval (cm),Mean height upper 95% uncertainty interval (cm)
Afghanistan,AFG,Men,1896,161.1640953,154.4842849,167.7540333
Afghanistan,1897,161.19628600000001,154.5716033,167.6596179
Afghanistan,1898,161.2282966,154.70734030000003,167.6025756
Afghanistan,1899,161.2607274,154.8356438,167.5281131
Afghanistan,1900,161.2930682,154.9595395,167.5080771
Afghanistan,1901,161.325492,155.03168169999998,167.4744515
Afghanistan,1902,161.3583553,155.14767980000002,167.44846030000002
Afghanistan,1903,161.39121469999998,155.2310885,167.4008892
Afghanistan,1904,161.42398930000002,155.33179840000003,167.3514324
Afghanistan,1905,161.4567642,155.39052809999998,167.3131696
Afghanistan,1906,161.48940430000002,155.47449369999998,167.2871157
Afghanistan,1907,161.5229355,155.5257899,167.2748095
Afghanistan,1908,161.5572929,155.65999140000002,167.2721319
Afghanistan,1909,161.5924115,155.7641954,167.2656384
Afghanistan,1910,161.62888480000004,155.85242639999998,167.2643945
Afghanistan,1911,161.6678293,155.9101227,167.24865469999997
Afghanistan,1912,161.7097565,156.0101067,167.2188218
Afghanistan,1913,161.75449440000003,156.13101890000002,167.2206299
Afghanistan,1914,161.8015948,156.2367514,167.19197069999998
Afghanistan,1915,161.8512242,156.3504853,167.2451706
Afghanistan,1916,161.9029869,156.38720419999999,167.29259299999998
Afghanistan,1917,161.9569878,156.4770209,167.3376131
Afghanistan,1918,162.0126184,156.55667319999998,167.4315055
Afghanistan,1919,162.06922869999997,156.64550830000002,167.45999980000002
Afghanistan,1920,162.1259389,156.7436736,167.4965072
Afghanistan,1921,162.18265780000002,156.80980680000002,167.52880449999998
Afghanistan,1922,162.2384477,156.88850630000002,167.5662868
Afghanistan,1923,162.29381659999999,156.9315314,167.595815
Afghanistan,1924,162.34936069999998,156.9838842,167.6831459
Afghanistan,1925,162.40637560000002,157.06584009999997,167.71076430000002
Afghanistan,1926,162.46580930000002,157.12897130000002,167.7515062
Afghanistan,1927,162.5287465,157.20963400000002,167.8346763
Afghanistan,1928,162.5953253,157.29458219999998,167.89861069999998
Afghanistan,1929,162.66497869999998,157.3860982,167.9576138
Afghanistan,1930,162.73636630000001,157.46432869999998,168.032184
Afghanistan,1931,162.8085523,157.5407991,168.06814619999997
Afghanistan,1932,162.8802754,157.63138130000002,168.13595719999998
Afghanistan,1933,162.9518219,157.7083345,168.2256208
Afghanistan,1934,163.0234031,157.79981709999998,168.3140129
Afghanistan,1935,163.09491119999998,157.88220080000002,168.38922030000003
Afghanistan,1936,163.16596,157.96873180000003,168.46445380000003
Afghanistan,1937,163.2362977,158.0821788,168.5443014
Afghanistan,1938,163.305397,158.11257849999998,168.5950251
Afghanistan,1939,163.37299430000002,158.19211409999997,168.6749524
Afghanistan,1940,163.4389961,158.2355819,168.7209468
Afghanistan,1941,163.5041548,158.33506930000001,168.8029007
Afghanistan,1942,163.5700656,158.4046993,168.86002309999998
Afghanistan,1943,163.63828480000004,158.4567146,168.9377491
Afghanistan,1944,163.7087534,158.488334,168.9868655
Afghanistan,1945,163.78105280000003,158.6082455,169.0857542
Afghanistan,1946,163.855399,158.6945419,169.1342874
Afghanistan,1947,163.93109330000001,158.78015569999997,169.20501330000002
Afghanistan,1948,164.00738069999997,158.83468,169.27836169999998
Afghanistan,1949,164.08322869999998,158.8912955,169.3490369
Afghanistan,1950,164.1575871,158.97574369999998,169.42048
Afghanistan,1951,164.2290702,159.06795380000003,169.46776819999997
Afghanistan,1952,164.29671489999998,159.14484050000002,169.5200216
Afghanistan,1953,164.36061719999998,159.20187880000003,169.5947042
Afghanistan,1954,164.4204793,159.25265190000002,169.61260880000003
Afghanistan,1955,164.476951,159.3093941,169.6625556
Afghanistan,1956,164.5306042,159.3679226,169.69411619999997
Afghanistan,1957,164.5831243,159.4454632,169.7196144
Afghanistan,1958,164.6357244,159.5074935,169.7586011
Afghanistan,1959,164.68877880000002,159.5384009,169.7917
Afghanistan,1960,164.7427774,159.5656102,169.8269781
Afghanistan,1961,164.7971321,159.6161401,169.8585176
Afghanistan,1962,164.8498304,159.6852401,169.9033064
Afghanistan,1963,164.8991445,159.7506496,169.96355490000002
Afghanistan,1964,164.9436129,159.7616514,170.0335017
Afghanistan,1965,164.9831305,159.79125380000002,170.09608390000002
Afghanistan,1966,165.0189045,159.84579219999998,170.14796869999998
Afghanistan,1967,165.051753,159.91527150000002,170.23547480000002
Afghanistan,1968,165.0826591,159.98267969999998,170.26083290000003
Afghanistan,1969,165.1135003,160.01789519999997,170.3084241
Afghanistan,1970,165.14396259999998,160.0000196,170.3303859
Afghanistan,1971,165.1741494,160.01091359999998,170.3736575
Afghanistan,1972,165.2040674,160.03740480000002,170.367747
Afghanistan,1973,165.23382890000002,160.03590590000002,170.39596830000002
Afghanistan,1974,165.26311030000002,160.03766869999998,170.4316786
Afghanistan,1975,165.2911729,160.05352919999999,170.4682746
Afghanistan,1976,165.3169361,160.093942,170.490847
Afghanistan,1977,165.33897819999999,160.1258136,170.5208936
Afghanistan,1978,165.3567716,160.114247,170.55957450000002
Afghanistan,1979,165.3707432,160.1044788,170.59697319999998
Afghanistan,1980,165.3812914,160.10441,170.5870316
Afghanistan,1981,165.38963569999999,160.1311798,170.6333142
Afghanistan,1982,165.3959387,160.15828080000003,170.6691401
Afghanistan,1983,165.3993526,160.1412397,170.6976905
Afghanistan,1984,165.39996340000002,160.1397253,170.7390584
Afghanistan,1985,165.39712219999998,160.10240259999998,170.769541
Afghanistan,1986,165.39091480000002,160.08017519999999,170.803145
Afghanistan,1987,165.38123969999998,160.00156719999998,170.80197859999998
Afghanistan,1988,165.3687054,159.9593264,170.8342272
Afghanistan,1989,165.3550563,159.9434535,170.84776630000002
Afghanistan,1990,165.340645,159.85148610000002,170.9242622
Afghanistan,1991,165.32616480000002,159.7783097,171.0055052
Afghanistan,1992,165.31207609999998,159.7288048,171.0550935
Afghanistan,1993,165.29810319999999,159.60550600000002,171.1128004
Afghanistan,1994,165.28414840000002,159.5093065,171.15373730000002
Afghanistan,1995,165.2701323,159.4186801,171.20187969999998

解决方法

  • 从原始数据文件中提取japan.csv数据时,似乎以错误的格式保存了数据。
    • 每行是用双引号引起来的单个字符串。
  • 使用pandas.read_csv读取原始文件。默认分隔符为sep=','
  • 使用Pandas: Indexing and selecting data选择日本的数据。
    • 可以使用japan.to_csv('japan.csv',index=False)保存一个单独的文件
  • 使用seaborn.lineplot并指定hue='Sex'进行绘图。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# global plt style parameters
plt.style.use('seaborn')
plt.rcParams['figure.figsize'] = (16.0,10.0)

# read in the file
df = pd.read_csv('NCD_RisC_eLife_2016_height_age18_countries.txt')

# display(df.head(3))
       Country  ISO  Sex  Year of birth  Mean height (cm)  Mean height lower 95% uncertainty interval (cm)  Mean height upper 95% uncertainty interval (cm)
0  Afghanistan  AFG  Men           1896        161.164095                                       154.484285                                       167.754033
1  Afghanistan  AFG  Men           1897        161.196286                                       154.571603                                       167.659618
2  Afghanistan  AFG  Men           1898        161.228297                                       154.707340                                       167.602576

# select data for japan
japan = df[df.Country == 'Japan']

# plot japan
sns.lineplot('Year of birth','Mean height (cm)',data=japan,hue='Sex')

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-