Skip to content Skip to sidebar Skip to footer

Merging Several Columns In One New Column In The Same Pandas Dataframe

So i have 5 columns in a dataframe that i like merge as a list in an existing column. A subset of the dataframe is below: loclize_indices_region i1 i2 i3

Solution 1:

Assuming df is your DataFrame with survey ID being an index you could do this in one line:

df['loclize_indices_region'] = df.loc[:,df.columns.tolist()[1:]].apply(lambda x: x.dropna().tolist(), 1)

or provide a list of columns separately:

cols = ['i1', ... ,'i5']
df['loclize_indices_region'] = df.loc[:,cols].apply(lambda x: x.dropna().tolist(), 1)

Note that column names are strings so i1 should be 'i1' in your list of columns.

OR, if you must to use external function:

def merge_cols(x, cols):
    return x.loc(axis=1)[cols].dropna().tolist()

cols = df.columns.tolist()[1:]

df['loclize_indices_region'] = df.apply(merge_cols, cols=cols, axis=1)

Post a Comment for "Merging Several Columns In One New Column In The Same Pandas Dataframe"