0

The dataframe example:

    col1, col2, col3
1     v1
2     v2
3           v3
4           v4
5                 v5
6                 v6

I want to merge col1, col2, col3 to the new column like the following:

    col_new
1      v1
2      v2
3      v3
4      v4
5      v5
6      v6

If the dataframe is the following format:

    col1, col2, col3
1     v1          v7
2     v2
3           v3
4           v4
5                 v5
6                 v6

Then, I will not merge these three columns. According to the three columns are not completely complementary. (row - index 1)


Currently, my method transforms three column to list and combine by zip(col1,col2,col3). Then, check each iteration in the list - [x[iteration] for x in list(zip(col1,...))]

Whether "Not Null" item is only one (=1) or not in each iteration? But it seems like an inefficient way.

Is there any efficient way to achieve my requirement?

tplandeer
  • 3
  • 2

1 Answers1

1

You can use :

df = df.fillna('').sum(axis=1)

# 0    v1
# 1    v2
# 2    v3
# 3    v4
# 4    v5
# 5    v6
Sruthi
  • 2,908
  • 1
  • 11
  • 25
  • Thanks for your reply. Your answer can merge perfectly when the row in all of the columns are complementary. But if there are two value in the same row, is there any way to detect and avoid to merge these three columns? – tplandeer Oct 21 '18 at 13:37
  • @tplandeer Try `df.fillna('').apply(lambda x: ','.join(x),1).str.strip(',').str.split(',,').apply(pd.Series)` – Abhi Oct 21 '18 at 14:02