Collapse multiple Columns in Pandas

Collapse multiple Columns in Pandas

To combine the contents of multiple columns into a single column, then you can do so using various methods in pandas. Here are a few approaches:

1. Using string concatenation:

If the columns you want to collapse/combine contain string data, you can simply concatenate them.

import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'A': ['a1', 'a2', 'a3'], 'B': ['b1', 'b2', 'b3'], 'C': ['c1', 'c2', 'c3'] }) df['D'] = df['A'] + df['B'] + df['C'] print(df) 

2. Using astype(str) for non-string columns:

If the columns contain non-string data, convert them to string first.

df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9] }) df['D'] = df['A'].astype(str) + df['B'].astype(str) + df['C'].astype(str) print(df) 

3. Using the .agg() function:

This method allows for more flexibility. You can use different functions to collapse the columns, not just concatenation.

df['D'] = df[['A', 'B', 'C']].agg(' '.join, axis=1) print(df) 

4. Using a custom function:

You can define a custom function to determine how the columns should be combined and then apply it row-wise.

def collapse(row): return f"{row['A']}-{row['B']}({row['C']})" df['D'] = df.apply(collapse, axis=1) print(df) 

Choose the method that best suits your specific needs. Each of the above methods results in a new column 'D' that contains the combined/collapsed values of columns 'A', 'B', and 'C'.


More Tags

homebrew java.util.logging spring-cloud create-guten-block seo sparse-checkout build-process findbugs flutter-packages sunburst-diagram

More Programming Guides

Other Guides

More Programming Examples