我有以下数据帧:
列numerote介于5
到25
之间。
我想在numerote更改数值并执行以下操作时,为每个数据创建一个导出csv
文件:
for i in range(5,26):
print(i)
a = racimitos[racimitos['numeroLote']==i][['peso','fecha','numeroLote']]
a.to_csv('racimitos{}.csv'.format(i), sep=',', header=True, index=True)
然后,我得到的数据集类似于:
会生成一个附加的列,就像上面红色框中的列一样
我尝试用以下方法删除此列:
^{pr2}$但我得到一个错误:
KeyError Traceback (most recent call last)
<ipython-input-18-e3ad718d5396> in <module>()
9 a = racimitos[racimitos['numeroLote']==i][['peso','fecha','numeroLote']]
10 a.to_csv('racimitos{}.csv'.format(i), sep=',', header=True, index=True)
---> 11 a.drop(columns=[' '], axis=1,)
~/anaconda3/envs/sioma/lib/python3.6/site-packages/pandas/core/indexes/base.py in drop(self, labels, errors)
4385 if errors != 'ignore':
4386 raise KeyError(
-> 4387 'labels %s not contained in axis' % labels[mask])
4388 indexer = indexer[~mask]
4389 return self.delete(indexer)
KeyError: "labels [' '] not contained in axis"
如何删除执行导出to.csv
时生成的空列索引?在
相反,您需要
index=False
,如下所示:另外,我认为在打印到.csv文件时没有必要包含
numeroLote
列,因为您在文件名中捕获了它的值。在以下是IMO使用
^{pr2}$groupby()
更有效的解决方案:您可以选择从索引1开始的所有列,而不是尝试删除未命名的列。在
相关问题 更多 >
编程相关推荐