Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot replace [''] with method pad on a DataFrame

Tags:

python

pandas

running python2.7

Question1

I want to replace empty string '' with None in my dataframe "test":

    from numpy.random import randn
    test = pd.DataFrame(randn(3,2))
    test.iloc[0,0]=''
    test.replace('', None)

Give me error TypeError:

cannot replace [''] with method pad on a DataFrame.

What went wrong?

Question 2:

from numpy.random import randn

test = pd.DataFrame(randn(3,2))
# this works
test.iloc[0,1]= 'A'
test[1] = test[1].replace('A','b')
# this does not
test.iloc[0,0]=''
test.loc[0] = test[0].replace('', None)


test 
          0         1
0                   b
1  0.042052  -1.44156
2  0.462131 -0.303288

I am expecting

test 
          0         1
0    None           b
1  0.042052  -1.44156
2  0.462131 -0.303288
like image 308
Lisa Avatar asked Mar 21 '26 16:03

Lisa


2 Answers

None is being interpreted as a lack of an argument. Try:

test.replace({'':None})

Question 2:

test.where(test != '', None)
like image 112
it's-yer-boy-chet Avatar answered Mar 23 '26 07:03

it's-yer-boy-chet


Just use not-a-number. Pandas would convert your None into not-a-number anyways*. Second, do not forget to assign your result to a new variable or set the inplace paramater to True instead. test.replace('', np.NaN) does nothing on its own.

import pandas as pd
import numpy as np
from numpy.random import randn
test = pd.DataFrame(randn(3,2))
test.iloc[0,0]=''
test = test.replace('', np.NaN)

*Pandas works best and is most efficient when all the data has the same datatype. That's numpy.float64 in your case. np.NaN is also a numpy float. If you wanted to have None in your dataframe, everything would need to be stored as object datatype which is less efficient. np.NaN is probably what you need.

like image 27
Joooeey Avatar answered Mar 23 '26 07:03

Joooeey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!