Saya memiliki kerangka data dan saya ingin menghapus baris yang berisi nilai non-numerik dan juga mengonversi float ke int
# Dataframe
productid
7819210
9600146.0
031351AA2
3255214
0018392MM
1785506
6924986
1915444
6007828.0
# cuurent approach
df.productid = df[df.productid.apply(lambda x: str(x).isnumeric())].astype(int) # It also remove floating point values because they contain decimal which is non-numeric
Saya juga membuat metode yang melakukan hal ini
def filternumeric(x):
if (re.search('\d+', str(x))): pass
else: return x
df.productid = df[df.productid.apply(filternumeric)].astype(int)
Tapi itu juga tidak berfungsi dengan benar. Ada saran yang lebih baik?
df[pd.to_numeric(df.productid, errors='coerce').notnull()]
- person cs95   schedule 05.08.2017