我正在写一个程序,它接受一个excel文件,重新排序列,丢弃碎片。一切都很好,除了当我试图重写一个函数,修复所有的列sio它是一个完整的混乱。结果如下:
def fixColsT(df1):
null_check = np.eye(len(df1.iloc[:,1]),12) #create matrix
null_check = map(np.isfinite, df1.iloc[:,1:12]) #set matrix-values.
null_check = map(np.invert, df1.iloc[:,1:12]) #Invert for error-checking (not_added)
df1.iloc[:,3:4] = map(str,df1.iloc[:,2:4]) #The problem.
df1.iloc[:,3] = map(lambda entry: re.sub(r'[^0-9]', '', entry), df1.iloc[:,3])
df1.iloc[:,4] = map(lambda x:x [len(x)-1], df1.iloc[:,4])
df1.iloc[:,4] = map(lambda x: re.sub(r'[^A-Ö]', '', x), df1.iloc[:,4])
df1.iloc[:,2] = (df1.iloc[:,2] - df1.iloc[:,4]) - df1.iloc[:,3]
df1.iloc[:,11] = map(fixnrRooms, df1.iloc[:,11])
df1.iloc[:,12] = map(fixKitchen, df1.iloc[:,12])
df1.columns = COLUMN_NAMES
errorcheck = null_check.iloc[:,2]
for i in CHECKS:
errorcheck = errorcheck + null_check[:,CHECKS[i]]
errors = np.where(errorcheck)[0]
print(df1.iloc[4,:])
return df1, errors
当我运行这个时,我得到以下错误:
Traceback (most recent call last):
File "[...]\main.py", line 403, in <module>
main()
File "[...]\main.py", line 391, in main
df, errors=fixColsT(dfInput)
File "[...]\main.py", line 118, in fixColsT
df1.iloc[:,3:4] = map(str,df1.iloc[:,2:4])
File "[...]\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 818, in __setitem__
iloc._setitem_with_indexer(indexer, value, self.name)
File "[...]\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1795, in _setitem_with_indexer
self._setitem_with_indexer_split_path(indexer, value, name)
File "[...]\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1836, in _setitem_with_indexer_split_path
elif len(ilocs) == 1 and lplane_indexer == len(value) and not is_scalar(pi):
TypeError: object of type 'map' has no len()
老实说,我不知道如何继续(请记住,我对python一般来说是相当陌生的),因为我需要以某种方式将这些列中的条目转换为字符串,以便其余代码工作。(以前的解决方案迭代了整个 Dataframe 的每一行,并手动转换了每个条目,这对于非常小的文件来说很好,但由于可以预期得到大约60000行的文件,这是不合理的)。如果不单独迭代每个条目或每个列,如何解决这个问题(除非绝对必要)。
1条答案
按热度按时间8aqjt8rx1#
看看这条线:
左手侧是 Dataframe 的(列)切片,并且右手侧是在 Dataframe 上应用函数的Map。两条评论:
map
在dataframe上不能很好地工作,如果你想沿着行,列或每个条目应用一个函数,它是模糊的(我认为这就是你想要的)。使用DataFrame.apply
或DataFrame.applymap
代替,map
用于系列。=
想要现在复制的内容的长度(甚至形状),这不是从map中可以读取的(你需要耗尽它并存储它的所有值)。总而言之,尝试使用: