Home > other >  How to compacting anything not null in pandas
How to compacting anything not null in pandas

Time:09-03

Here's my input

code    US    UK    Germany   Japan
AR5      13   NaN         7    NaN
A85     NaN     9       NaN      8

Here's my Output, anything not null will be registered

code  country
A85       UK
A85    Japan
AR5       US
AR5   Germany

Regards

CodePudding user response:

You can melt then dropna:

df.melt('code', var_name='Country').dropna()

Output:

  code  Country  value
0  AR5       US   13.0
3  A85       UK    9.0
4  AR5  Germany    7.0
7  A85    Japan    8.0
  • Related