本文介绍了切片数据框时出现KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码如下:
d = pd.read_csv('Collector Output.csv')
df = pd.DataFrame(data=d)
dfa = df.copy()
dfa = dfa.rename(columns={'OBJECTID': 'Object ID', 'test_no': 'TEST #', 'retest_no': 'RETEST #',
'tester': 'TESTED BY', 'date': 'DATE', 'proj_no': 'Project Number',
'test_elev': 'TEST ELEVATION', 'curve_no': 'CURVE #', 'curve':
'Curve Description', 'dry': 'Dry Maximum Density (pcf)', 'opt': 'OPT. M.C.',
'Material Type': 'Material Type', 'AC Thickness': 'AC Thickness', 'AB Thickness': 'AB Thickness',
'Wet In-place Density, pcf)': 'Wet In-place Density (pcf)', 'inplace_mo': 'FIELD M.C.',
'comp_dryde': 'DRY DENSITY', 'relative_c': 'R.C.', 'addl_notes': 'NOTES',
'material_t': 'Material Type', 'wet_inpac': 'Wet In-place', 'comp_dryde': 'comp_dryde', 'relative_c': 'relative compaction',
'GlobalID': 'GlobalID', 'GlobalID_2': 'GlobalID 2', 'x': 'EAST', 'y': 'NORTH'})
writer = pd.ExcelWriter('Test Test.xlsx', engine= 'xlsxwriter')
dfa.to_excel(writer, sheet_name='Output')
writer.save()
#Slice the dataframe to get the desired columns by their 'name'
dfb = dfa[['TEST #', 'DATE', 'TESTED BY', 'NORTH', 'EAST', 'TEST ELEVATION', 'CURVE #', 'OPT. M.C.', 'FIELD M.C.',
'DRY DENSITY', 'R.C.', 'RETEST #', 'NOTES']]
当我运行它时,我得到一个KeyError,它读取的是['DRY DENSITY''R.C.']而不是索引.我确认名称对于.csv中的列是正确的.为什么这两列都出了问题?
When I run it, I get a KeyError that reads ['DRY DENSITY' 'R.C.'] not index. I varified that the names are correct for the columns in the .csv. Why are these two columns giving an issue?
推荐答案
我认为需要加倍[[]]
:
cols = ['TEST #', 'DATE', 'TESTED BY', 'NORTH', 'EAST', 'TEST ELEVATION', 'CURVE #', 'OPT. M.C.', 'FIELD M.C.', 'DRY DENSITY', 'R.C.', 'RETEST #', 'NOTES']
dfb = dfa[cols]
如果某些列有误,请添加 intersection
:
If some columns are mising add intersection
:
dfb = dfa[dfa.columns.intersection(cols)]
这篇关于切片数据框时出现KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!