我有一张表格,其中列出了行发票,行赔偿类型以及引擎功能
引擎的动力始终包含符号'/'
因此可以过滤我拥有的索引:
我希望有一个新行,其中包含每张发票的不同权力列表
例如,对于“ inv123”,新行应包含['400 / HP','500kw / h']
到目前为止,我有以下代码: 从itertools导入压缩
boolean_filter = DF.index.str.contains('/') & DF['inv123']
indexlist =list(DF.index)
mylist = list(compress(indexlist, boolean_filter))
# you can generate it in one liner
mylist = list(compress(DF.index,DF.index.str.contains('/') & DF['inv123']))
print(mylist)
结果
['400/HP', '500/kwh']
这是我必须在row ='concatenate“ column ='inv123'中添加的值
我遇到很多问题
b)添加带有以下内容的空行:
DF.append(pd.Series(name ='concatenate'))
0、1s(整数)的dtype变为float,这使得代码不可重用(不再是布尔值)
有人知道如何解决这个问题?
但是我仍然必须遍历每一列
答案 0 :(得分:0)
我想出了这个解决方案
from itertools import compress
lc=[list(compress(DF.index,DF.index.str.contains('/') & DF.iloc[:,i])) for i in range(len(DF.columns))]
首先要用每列的布尔值(DF.iloc [:,i])压缩索引列表
结果,我获得了一个列表,其中每个元素都是所需值的列表。
解决方案并不完美。 我花了几个小时。