-3

I have column in my dataframe which is having string value as shown in fig 1.

enter image description here

What i wanted to do is to replace all nan value from 0 and other with 1 (whatever another field is like string and int) I tried this

func_lambda = lambda x: 1 if any(dataframe['Colum'].values != 0) else 0

But t is replacing all the column with 1.

this is my df.head

datacol.head(20)
Out[77]: 
0                          nan
1                   4500856427
2                      4003363
3                          nan
4                      16-4989
5                          nan
6                          nan
7              WVE-78686557032
8                          nan
9                   4501581113
10    D4-SC-0232737-1/G1023716
11                         nan
12                         nan
13                  4502549104
14                         nan
15                         nan
16                         nan
17                    IT008297
18                   15\036628
19                   299011667
Name: Customer_PO_Number, dtype: object
Imran Ahmad Ghazali
  • 605
  • 1
  • 10
  • 16

2 Answers2

1

Check this:

import pandas as pd

df = pd.DataFrame({"Customer_PO_Number":
                       ['nan','4500856427','4003363','nan','16 - 4989','nan','nan','WVE - 78686557032',
                        'nan','4501581113','D4 - SC - 0232737 - 1 / G1023716','nan','nan','4502549104',
                        'nan','nan','nan','IT008297','15\03662','8','299011667']})


df.replace('nan', 0, inplace=True)  # for replacing nan to 0

df[df != 0] = 1     # for replacing others to 1
print(df)

It will give you output like this:

   Customer_PO_Number
0                   0
1                   1
2                   1
3                   0
4                   1
5                   0
6                   0
7                   1
8                   0
9                   1
10                  1
11                  0
12                  0
13                  1
14                  0
15                  0
16                  0
17                  1
18                  1
19                  1
20                  1

Hope it will help you! :)

Abdullah Ahmed Ghaznavi
  • 1,978
  • 3
  • 17
  • 27
  • Using fillna might be the way to go here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html – dpwr Apr 26 '18 at 14:31
  • 1
    @dpwrussell : i tried that to but it seems like that fillna replace NaN but here we have nan thats why i give this solution! – Abdullah Ahmed Ghaznavi Apr 27 '18 at 06:19
1

You can use a boolean test and cast the result as integer:

(df['Customer_PO_Number'] == 'nan').astype(int)

Output:

0     1
1     0
2     0
3     1
4     0
5     1
6     1
7     0
8     1
9     0
10    0
11    1
12    1
13    0
14    1
15    1
16    1
17    0
18    0
19    0
20    0
Name: Customer_PO_Number, dtype: int32

If 'nan' are really np.nan then you can use isnull:

df['Customer_PO_Number'].isnull().astype(int)
Scott Boston
  • 147,308
  • 15
  • 139
  • 187