-1

I have a dataframe like this.

   Project 4    Project1    Project2    Project3
0   NaN         laptio          AB      NaN
1   NaN         windows         ten     NaN
0   one         NaN             NaN
1   two         NaN             NaN

I want to delete NaN values from Project 4 column

My desired output should be,

df,

   Project 4    Project1    Project2    Project3
0   one         laptio          AB      NaN
1   two         windows         ten     NaN
0                   NaN        NaN       NaN
1                NaN            NaN
Pyd
  • 6,017
  • 18
  • 52
  • 109
  • 1
    Possible duplicate of [Remove NaN 'Cells' without dropping the entire ROW (Pandas,Python3)](https://stackoverflow.com/questions/25941979/remove-nan-cells-without-dropping-the-entire-row-pandas-python3) – error Jul 31 '17 at 10:58
  • Where does the third `NaN` in column `Project3` come from? And cells with empty representation I guess contain empty strings? – Stop harming Monica Jul 31 '17 at 11:04
  • yes @Goyo, below answer worked for me. Thank you – Pyd Jul 31 '17 at 11:07

1 Answers1

2

If your data frame's index is just standard 0 to n ordered integers, you can pop the Project4 column to a series, drop the NaN values, reset the index, and then merge it back with the data frame.

import pandas a pd

df = pd.DataFrame([[pd.np.nan, 1,2,3],
                   [pd.np.nan, 4,5,6],
                   ['one',7,8,9],
                   ['two',10,11,12]], columns=['p4','p1','p2','p3'])

s = df.pop('p4')
pd.concat([df, ps.dropna().reset_index(drop=True)], axis=1)

# returns:
   p1  p2  p3   p4
0   1   2   3  one
1   4   5   6  two
2   7   8   9  NaN
3  10  11  12  NaN
James
  • 32,991
  • 4
  • 47
  • 70