Reputation: 231
I have a pandas dataframe with only one row and hundreds of columns and I want to create a list of strings where the elements are the column names of the dataframe for which the value meet a condition. I also want to concatenate a specific text to each element. Here's the dataframe:
df = pd.DataFrame(data={
'name': ["Jimmy"],
'SN522': [0],
'SN521': [1],
'SN520': [0],
'SN519': [1],
'SN518': [0]})
I want all the column names where the row = 0, and adding the text "AN" at the beginning, and all separed by "-" The expected output would be this character:
cols0 = "ANSN522 - ANSN520 - ANS518"
In R it would be something like this (not sure):
column_0 <- paste(paste0("AN",c(colnames(df)[which(df[1,]==0)])),sep="-")
I tried first step using a for loop:
column_0 = [i for i in df.columns if df.i == 0]
but I have an error: AttributeError: 'DataFrame' object has no attribute 'i'
I haven't figured out how to solve it yet, do you have an idea?
Upvotes: 1
Views: 532
Reputation: 18416
Filter out the values that are 0 for each rows applying lambda function on axis=1
, then take the index of the filtered values, concatenate AN
to each index values (i.e. the column index/column names), then join them by -
:
>>> df.apply(lambda x: ' - '.join('AN'+i for i in x[x==0].index), axis=1)
0 ANSN522 - ANSN520 - ANSN518
dtype: object
It will generate these strings for each of the rows individually.
Upvotes: 1