[英]Python + Pandas + Dataframe : Keyerror
I have about 190 CSV's.我有大约 190 个 CSV。 each of which has same column names.每个都有相同的列名。 A sample csv shared below:下面分享了一个示例 csv:
From every csv, I need to pick only the Item
, Predicted_BelRd(D2)
, Predicted_Ulsoor(D2)
, Predicted_ChrchStrt(D2)
, Predicted_BlrClub(D2)
, Predicted_Indrangr(D1)
, Predicted_Krmngl(D1)
, Predicted_KrmnglBkry(D1)
, Predicted_HSR(D1)
columns of only the first row , and need to store all these rows to a separate CSV.从每一个CSV,我需要选择只Item
, Predicted_BelRd(D2)
Predicted_Ulsoor(D2)
Predicted_ChrchStrt(D2)
Predicted_BlrClub(D2)
Predicted_Indrangr(D1)
Predicted_Krmngl(D1)
Predicted_KrmnglBkry(D1)
Predicted_HSR(D1)
列只有第一行,并且需要将所有这些行存储到单独的 CSV 中。 So the final CSV should 190 rows.所以最终的 CSV 应该是 190 行。
For that I wrote a code:为此,我写了一段代码:
path = '/home/hp/products1'
all_files = glob.glob(path + "/*.csv")
#print(all_files)
columns = ['Item', 'Predicted_BelRd(D2)', 'Predicted_Ulsoor(D2)', 'Predicted_ChrchStrt(D2)', 'Predicted_BlrClub(D2)', 'Predicted_Indrangr(D1)', 'Predicted_Krmngl(D1)', 'Predicted_KrmnglBkry(D1)', 'Predicted_HSR(D1)']
#columns = []
#df.iloc[:, np.r_[1:10, 15, 17, 50:100]]
rows_list = []
for filename in all_files:
origin_data = pd.read_csv(filename)
my_data = origin_data[columns]
rows_list.append(my_data.head(1))
output = pd.DataFrame(rows_list)
#output.to_csv(file_name, sep='\t', encoding='utf-8')
output.to_csv('smallys_final.csv', encoding='utf-8', index=False)
It gives the following error:它给出了以下错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-7-229c30dd03e1> in <module>()
9 for filename in all_files:
10 origin_data = pd.read_csv(filename)
---> 11 my_data = origin_data[columns]
12 rows_list.append(my_data.head(1))
13
~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
2131 if isinstance(key, (Series, np.ndarray, Index, list)):
2132 # either boolean or fancy integer index
-> 2133 return self._getitem_array(key)
2134 elif isinstance(key, DataFrame):
2135 return self._getitem_frame(key)
~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/frame.py in _getitem_array(self, key)
2175 return self._take(indexer, axis=0, convert=False)
2176 else:
-> 2177 indexer = self.loc._convert_to_indexer(key, axis=1)
2178 return self._take(indexer, axis=1, convert=True)
2179
~/anaconda3/envs/tensorflow/lib/python3.5/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
1267 if mask.any():
1268 raise KeyError('{mask} not in index'
-> 1269 .format(mask=objarr[mask]))
1270
1271 return _values_from_object(indexer)
KeyError: "['Predicted_BelRd(D2)' 'Predicted_Ulsoor(D2)' 'Predicted_ChrchStrt(D2)'\n 'Predicted_BlrClub(D2)' 'Predicted_Indrangr(D1)' 'Predicted_Krmngl(D1)'\n 'Predicted_KrmnglBkry(D1)' 'Predicted_HSR(D1)'] not in index"
Content of one of those dataframes:这些数据帧之一的内容:
prod = pd.read_csv('/home/hp/products1/' + 'prod[' + str(0) + '].csv', engine='python')
print(prod)
Output:输出:
Category Item UOM BelRd(D2) Ulsoor(D2) \
0 Food/Bakery BAKING POWDER SPARSH (1KGS) PKT 0 0
1 Food/Bakery BAKING POWDER SPARSH (1KGS) PKT 0 0
2 Food/Bakery BAKING POWDER SPARSH (1KGS) PKT 0 0
3 Food/Bakery BAKING POWDER SPARSH (1KGS) PKT 0 0
4 Food/Bakery BAKING POWDER SPARSH (1KGS) PKT 0 0
ChrchStrt(D2) BlrClub(D2) Indrangr(D1) Krmngl(D1) KrmnglBkry(D1) \
0 0 0 0 0 1
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 1
HSR(D1) date Predicted_BelRd(D2) Predicted_Ulsoor(D2) \
0 0 10 FEB 19 0.0 0.0
1 0 17 FEB 19 NaN NaN
2 0 24 FEB 19 NaN NaN
3 0 4 MARCH 19 NaN NaN
4 0 11 MARCH 19 NaN NaN
Predicted_ChrchStrt(D2) Predicted_BlrClub(D2) Predicted_Indrangr(D1) \
0 0.0 0.0 0.0
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
Predicted_Krmngl(D1) Predicted_KrmnglBkry(D1) Predicted_HSR(D1)
0 0.0 0.0 0.0
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
4 NaN NaN NaN
Edit:编辑:
prod = pd.read_csv('/home/hp/products1/' + 'prod[' + str(0) + '].csv', engine='python')
print(list(prod))
Output:输出:
['Category', 'Item', 'UOM', 'BelRd(D2)', 'Ulsoor(D2)', 'ChrchStrt(D2)', 'BlrClub(D2)', 'Indrangr(D1)', 'Krmngl(D1)', 'KrmnglBkry(D1)', 'HSR(D1)', 'date', 'Predicted_BelRd(D2)', 'Predicted_Ulsoor(D2)', 'Predicted_ChrchStrt(D2)', 'Predicted_BlrClub(D2)', 'Predicted_Indrangr(D1)', 'Predicted_Krmngl(D1)', 'Predicted_KrmnglBkry(D1)', 'Predicted_HSR(D1)']
I think you need to remove the tab spaces and re-excute the code.我认为您需要删除制表符空间并重新执行代码。 It may work fine.它可能工作正常。 I think because of the unwanted tab spaces the dataframe is not able to distinguish the real values and outpus "KeyError".我认为由于不需要的制表符空间,数据框无法区分真实值和输出“KeyError”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.