我在ndf = df.melt(id_vars ='Country',var_name ='Year',value_name ='Obesity(%)'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我在ndf = df.melt(id_vars ='Country',var_name ='Year',value_name ='Obesity(%)'相关的知识,希望对你有一定的参考价值。
Traceback (most recent call last): File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc return self._engine.get_loc(key) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Country' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<ipython-input-57-97bd8d719664>", line 1, in <module> ndf = df.melt(id_vars='Country', var_name='Year', value_name='Obesity (%)') File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\frame.py", line 6506, in melt col_level=col_level, File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\reshape\melt.py", line 108, in melt id_data = frame.pop(col) File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\generic.py", line 790, in pop result = self[item] File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__ indexer = self.columns.get_loc(key) File "C:\Users\fachr\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Country'
**我的代码**
#cleaning and restructuring data
import numpy as np
import pandas as pd
df = pd.read_csv('data.csv')
df.head()
df.shape
df.iloc[0,1]
df.columns
df.drop([0,1,2],inplace=True)
df.head()
df.reset_index(drop=True, inplace = True)
df.head()
df.rename(columns='Unnamed : 0' : 'Country', inplace = True)
df.head
ndf = df.melt('Country', var_name='Year', value_name='Obesity (%)')
ndf[['Year', 'Sex']] = ndf['Year'].str.split('.', expand=True)
ndf.head(10)
ndf=ndf.sort_values(by=['Country','Year'])
ndf=ndf.reset_index(drop=True)
ndf.head()
ndf['Sex']=ndf['Sex'].map(None: 'Both sexes', '1': 'Male', '2':'Female')
ndf['Age standardized estimate']=ndf['Obesity (%)'].apply(lambda x:x.split()[1])
ndf['Obesity (%)']=ndf['Obesity (%)'].apply(lambda x:x.split()[0])
ndf.head()
ndf.to_csv('obesity-clean-split.csv')
答案
如文档中所述,id_vars
的pandas.DataFrame.melt关键字接受tuple
,list
或ndarray
作为输入,而不是如您所愿的字符串。您必须在其中包含密钥方括号使其成为列表
将引发错误的行更改为:
# Take note of the [] around 'Country'
ndf = df.melt(['Country'], var_name='Year', value_name='Obesity (%)')
以上是关于我在ndf = df.melt(id_vars ='Country',var_name ='Year',value_name ='Obesity(%)'的主要内容,如果未能解决你的问题,请参考以下文章