Home > other >  remove specific nan values from pandas dataframe
remove specific nan values from pandas dataframe

Time:06-14

I have the following table and I want to remove the NaN values and essentially have the two on the same row.

| ID         | Name   | NewID    |NewName|
| ---------- | ------ |----------|------|
| (int, 500) | Crosby |    NaN   |  NaN |
|    NaN     |   NaN  |(str, 500)|Sidney|

Is there any way to remove these and have the words in the same row in pandas and have it so that the output is like this?

| ID         | Name   | NewID     | NewName|
| ---------- | ------ |-----------|--------|
| (int, 500) | Crosby |(str, 500) | Sidney |

df=df.dropna would remove the whole row so that wouldn't work

CodePudding user response:

use fillna to forward fill the NaN values and then drop the NaN rows

df2=df.fillna(method="ffill")
df2.dropna()
df2
    ID          Name    NewID       NewName
1   (int,500)   Crosby  (str,500)   Sidney

CodePudding user response:

If you data is static you could use a shift to push everything into the correct row

df = pd.DataFrame({
    'ID' : [1, np.nan],
    'Name' : ['Crosby', np.nan],
    'NewID' : [np.nan, 2],
    'NewName' : [np.nan, 'Sidney']
})
df[['NewID', 'NewName']] = df[['NewID', 'NewName']].shift(-1)
df = df.dropna()
df
  • Related