Reputation: 387
I have a dataframe like this:
a|b|c|d
1 true|false|true|false
2 false|true|true|false
3 true|true|true|false
I want this to be structured as :
a,c
b,c
a,b,c
and exported to a text file. How can we do that? I tried and could do the opposite but not this.Could you give me some idea?
Upvotes: 0
Views: 49
Reputation: 59579
You could modify the column names, use dot
(As True == 1 and False == 0), and then get rid of the trailing comma.
#df = df.replace({'true': True, 'false': False}) # If strings, not Bools
df.columns = [f'{x},' for x in df.columns]
df.dot(df.columns).str.strip(',')
#0 a,c
#1 b,c
#2 a,b,c
#dtype: object
Or more plainly, string join the columns after slicing by the Boolean row Series:
pd.Series([','.join(df.columns[x]) for _,x in df.iterrows()],
index=df.index)
#0 a,c
#1 b,c
#2 a,b,c
#dtype: object
Upvotes: 3
Reputation: 109666
First, get the column names by splitting the first column on the |
symbol.
Then split the values in that column on the same symbol, and use a conditional comprehension to select the corresponding columns where the split value is true
.
Save the result back to a .csv
file without the index.
cols = df.columns[0].split('|')
(df
.iloc[:, 0].str
.split('|')
.apply(lambda bools: ','.join(col for col, b in zip(cols, bools) if b == 'true'))
.to_csv(filename, index=False)
)
Upvotes: 1