Home > other >  dataframe duplicate column values ​in one column
dataframe duplicate column values ​in one column

Time:11-03

I have the following dataframe

column1 column1
None value 1
None value 1
None value 1
None value 1
value2 None

Both columns have the same name. I want to make these two columns into one column and fill them with values. like this

column1
value1
value1
value1
value1
value2

What should I do?

Note that only one of the duplicate columns has a value.

CodePudding user response:

Replace None string values by NaN float values, then stack which will convert the data to a single column dropping na values as dropna=True by default for stack, then finally unstack:

df.replace('None', float('nan')).stack().unstack()

OUTPUT:

   column1
0  value 1
1  value 1
2  value 1
3  value 1
4   value2
  • Related