简体   繁体   English

python pandas - 如何为每一行创建一个带有条件的列名列表?

[英]python pandas - how to create for each row a list of column names with a condition?

I need apply a function to all rows of dataframe I have used this function that returns a list of column names if value is 1:我需要将 function 应用到 dataframe 的所有行中

def find_column(x):  
    a=[]  
    for column in df.columns:  
        if (df.loc[x,column] == 1):  
            a = a + [column]
    return a

it works if i just insert the index, for example:如果我只插入索引,它就可以工作,例如:

print(find_column(1))

but:但:

df['new_col'] = df.apply(find_column,axis=1)

does not work any idea?没有任何想法? Thanks!谢谢!

You can iterate by each row, so x is Series with index same like columns names, so is possible filter index matched data and convert to list:您可以按每一行进行迭代,因此x是具有与列名称相同的indexSeries ,因此可以过滤索引匹配的数据并转换为列表:

df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,1,4,5,5,1],
         'C':[7,1,9,4,2,3],
         'D':[1,1,5,7,1,1],
         'E':[5,1,6,9,1,4],
         'F':list('aaabbb')
})

def find_column(x):
    return x.index[x == 1].tolist()

df['new'] = df.apply(find_column,axis=1)
print (df)
   A  B  C  D  E  F           new
0  a  4  7  1  5  a           [D]
1  b  1  1  1  1  a  [B, C, D, E]
2  c  4  9  5  6  a            []
3  d  5  4  7  9  b            []
4  e  5  2  1  1  b        [D, E]
5  f  1  3  1  4  b        [B, D]

Another idea is use DataFrame.dot with mask by DataFrame.eq for equal, then remove last separator and use Series.str.split :另一个想法是使用DataFrame.dot与掩码DataFrame.eq相等,然后删除最后一个分隔符并使用Series.str.split

df['new'] = df.eq(1).dot(df.columns + ',').str.rstrip(',').str.split(',')
print (df)

   A  B  C  D  E  F           new
0  a  4  7  1  5  a           [D]
1  b  1  1  1  1  a  [B, C, D, E]
2  c  4  9  5  6  a            []
3  d  5  4  7  9  b            []
4  e  5  2  1  1  b        [D, E]
5  f  1  3  1  4  b        [B, D]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个新列,该列是一行中有多少条目满足pandas中数据帧的每一行条件的计数 - Create new column that is a count of how many entries in a row satisfy a condition for each row of a data frame in pandas Python pandas为每一行按降序查找列名 - Python pandas find the column names in descending order for each row 我如何使用 python pandas 数据框并使用列名和行名作为新列创建一个新表 - how do i take a python pandas dataframe and create a new table using the column and row names as the new column 获取每个索引(行)的列名,以便对熊猫中的某些条件施加列值 - Get the column names for each index(row) such that column value is imposed upon some condition in pandas Pandas 列表列,为每个列表元素创建一行 - Pandas column of lists, create a row for each list element Python / pandas:如何根据字符串条件递增每一行 - Python/pandas: How to increment each row based on string condition 在 Python 中使用列名和行名创建矩阵 - create matrix with column names and row names in Python 如何使用列名列表来获取 python 中每一列的索引 - how to use a list of column names to get the indices of each column in python 为每一行获取满足条件的列名列表的有效(可能是最短)方法 - Efficient (& perhaps shortest) way of getting a list of column names that satisfies a condition, for each row 从 python 中的 pandas dataframe 的每一列创建列表嵌套 - create list nest from each column of pandas dataframe in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM