Concatenate multiple columns of dataframe with a seperating character for Non-null values

Yash

I have a data frame like this:

df:
C1   C2  C3
1    4    6
2   NaN   9
3    5   NaN
NaN  7    3

I want to concatenate the 3 columns to a single column with comma as a seperator. But I want the comma(",") only in case of non-null value.

I tried this but this doesn't work for non-null values:

df['New_Col'] = df[['C1','C2','C3']].agg(','.join, axis=1)

This gives me the output:

New_Col
1,4,6
2,,9
3,5,
,7,3

This is my ideal output:

New_Col
1,4,6
2,9
3,5
7,3

Can anyone help me with this?

New England cottontail

Judging by your (wrong) output, you have a dataframe of strings and NaN values are actually empty strings (otherwise it would throw TypeError: expected str instance, float found because NaN is a float).

Since you're dealing with strings, pandas is not optimized for it, so a vanilla Python list comprehension is probably the most efficient choice here.

df['NewCol'] = [','.join([e for e in x if e]) for x in df.values]

result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Concatenate multiple columns, with null values

Concatenate two columns of spark dataframe with null values

Seperating same values into columns

How do you concatenate multiple columns in a DataFrame into a another column when some values are null?

Selecting values from non-null columns in a PySpark DataFrame

How to fill Non-Null values from some columns in Pandas Dataframe into a new column? How to use np.where() for multiple conditions?

How to assign values to multiple non existing columns in a pandas dataframe?

How to concatenate null columns in spark dataframe in java?

How to concatenate last non-blank cells values from multiple columns

How to concatenate two columns of spark dataframe with null values but get one value

Concatenate/paste together multiple columns in a dataframe

Concatenate values into multiple columns based on associated value

Concatenate multiple values and removing characaters when null

concatenate values in dataframe if a column has specific values and None or Null values

Pandas DataFrame, divide a column having multiple values into multiple columns and remove null values

Reshape dataframe with multiple values columns

Split dataframe on multiple columns and values

Get multiple columns with null values

Seperating a character string

Extract multiple columns and add null character in between

grouping multiple columns and bringing values into character string

counting character values in multiple columns in R

Create a column in dataframe using lambda based on another columns with non-null values

transform a big dataframe with many None values to smaller one with indication of non null columns

Pyspark: How to return a tuple list of existing non null columns as one of the column values in dataframe

How to pick out all non-NULL value from multiple columns in Python Dataframe

How to conditionally select the first non null date from multiple datetime columns in a pandas dataframe?

How to concatenate two columns that might contain NULL values in a select statement?

Check multiple columns for multiple values and return a dataframe