I have some sample data here. What the code is supposed to do is to remove any words inside. However, occasionally mongo throws in a Null value or an empty string.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1],[4],[25],['asd123'],['asdf1']]),
columns=['account'])
df["account"] = (
df["account"]
.fillna((-1), inplace=False)).astype("str").str.replace(r"[^0-9]", "", regex=True).astype('int64')
I have tried replacing it with an integer below, but Python keeps throwing me an error ValueError: invalid literal for int() with base 10: ''
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1],[4],[25],['asd123'],['']]),
columns=['account'])
np.where(df.applymap(lambda x: x == ''))
df.replace('', '1')
df["account"] = (
df["account"]
.fillna((-1), inplace=False)).astype("str").str.replace(r"[^0-9]", "", regex=True).astype('int64')
df
I have also tried and it doesnt work too:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1],[4],[25],['asd123'],['']]),
columns=['agent_account'])
np.where(df.applymap(lambda x: x == ''))
df.replace('','abc1',regex = True)
Any help is appreciated. Thanks!
CodePudding user response:
Because you're trying to convert "" to an integer. Just delete the "" when getting the data, with this line df.drop(df[df.account == ""].index, inplace=True)
This would be your modified code
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([[1],[4],[25],['asd123'],['']]),
columns=['account'])
np.where(df.applymap(lambda x: x == ''))
df.drop(df[df.account == ""].index, inplace=True)
df["account"] = (
df["account"]
.fillna((-1), inplace=False)).astype("str").str.replace(r"[^0-9]", "", regex=True).astype('int64')