In the following pandas Dataframe:
alfa alfa_id beta beta_id
ak 23 ji 24
bc 24 kl 25
I want to convert the columns alfa and beta to list.
cols = [l for l in df.columns.values if not l.endswith('id')]
df = df[cols].applymap(lambda c:[list(c)])
# gives me
Output I am getting now:
print(df)
alfa beta
[[a, k]] [[j, i]]
** But, Expected output is:**
alfa alfa_id beta beta_id
[[a, k]] 23 [[j, i]] 24
- The columns for
alfa_id and beta_idare lost but I want them back in one line code (as simple as possible). - I am looking for a method which also keeps the memory foot print low.
PS note: I could have done
df = df.applymap(lambda c:[list(c)] if type(c) is str else c)
But, I don't want this because there will be some other columns which should not be converted to list. So, I wanted to make cols values specifically which needs to be converted to list.
Thanks,