我需要重置对象数据帧中的一些值
import pandas as pd
import numpy as np
df = pd.DataFrame({
'x': [[0] * 6, ['']],
'y': [[0, np.nan, 0, 1, 0, 1], [4, 3, 5]],
'z': [[5, 5, 5, 4, 4, 4], [65, 324, 45.345]]
})
def test(ar):
if ar == ['']:
return False
else:
return True
df.applymap(test)
applymap返回:
我只需要将带有True
的列和单元格放入如下结构中:
d = {
'x': [(0, [''])],
'y': [(0, ['']), (1, ['']),]
'z': [(0, ['']), (1, ['']),]
}
实现这一目标的最快方法是什么?必须有一个简单的方法来解决它。你知道吗
也许有一种更聪明的方法可以做到这一点,但这可以完成任务:
asker算法的改进
我不确定这有多有效,但你可以试试:
输出:
由询问者优化算法。如果直接返回
applymap
中的正确对象,则不需要替换步骤相关问题 更多 >
编程相关推荐