[英]Iterate over pandas rows and get groups of values?
我有一个Df
看起来像:
marker | 0 1 2 3
________________
A | + - + -
B | - - + -
C | - + - -
我想遍历列并获取有+
的行的名称,即对所有+
行进行分组。
我试图通过以下方式做到这一点:
lis = []
for n in list(range(0,3)):
cli = Df[n].tolist()
for x,m in zip(cli,markers): # markers is a list of the row names ['A','B','C']
cl_li = []
if x == '+':
mset = m+x
cl_li.append(mset)
else:
continue
lis.append(cl_li)
print (lis)
但是我将每一行名称作为名称中的自己的子列表,而我想要的是:
newdf =
____________
0 | A+
1 | C+
2 | A+B+
#n.b group 3 not included
尝试在布尔矩阵上使用apply
和join
:
(df == '+').apply(lambda x: '+'.join(x.index[x])+'+').to_frame()
输出:
0
marker
0 A+
1 C+
2 A+B+
或者,使用dot
和布尔矩阵:
(df.index.to_series()+'+').dot((df=='+'))
输出:
0
marker
0 A+
1 C+
2 A+B+
我的提议是使用比你更多的 Pandasonic 解决方案。
对每一列应用 lambda 函数:
result = df.apply(lambda col: ''.join(col[col == '+'].index + '+'))
要从结果中删除空项目,请运行:
result = result[result != '']
结果是:
0 A+
1 C+
2 A+B+
dtype: object
如果您希望结果为 DataFrame (而不是Series ),请运行:
result = result[result != ''].to_frame()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.