1

I have a dataframe say df_dt_proc with 35 columns.

I want to add a column to the dataframe df_dt_proc['procedures'] which should have all the columns concatenated except column at index 0 separated by , .

I am able to achieve the result by the following script:

df_dt_proc['procedures'] = np.nan

_len = len(df_dt_proc.columns[1:-1])

for i in range(len(df_dt_proc)):
    res = ''

    for j in range(_len):
        try:
            res += df_dt_proc[j][i] + ', '
        except:
            break
    
    df_dt_proc['procedures'][i] = res

However, there must be a more pythonic way to achieve this.

2 Answers 2

1

Use custom lambda function with remove NaN and Nones and converting to strings, for select all columns without first and last use DataFrame.iloc:

f = lambda x: ', '.join(x.dropna().astype(str))
df_dt_proc['procedures'] = df_dt_proc.iloc[:, 1:-1].agg(f, axis=1)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this with agg:

df_dt_proc['procedures'] = df_dt_proc[df_dt_proc.columns[1:-1]].astype(str).agg(', '.join, axis=1)

3 Comments

This is working fine. What if we have blanks values in a few columns? I am getting error - expected str instance, float found.
@gm-123 Edited my answer, please upvote and accept if it works
it appends nan for all the empty columns. Result looks like 'Atrial Septal Defect (ASD) Repair, Bentall Procedure, CABG - Redo, Cardiac Valve Replacement, Closed Heart Surgery, Coronary Artery Bypass Grafting (CABG), Heart Double Valve Replacement, Heart Port Surgery, Heart Transplant, VSD Closure / Repair (Adult), nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.