ฉันมี dataframe และฉันต้องการลบแถวที่มีค่าที่ไม่ใช่ตัวเลขและแปลง float เป็น 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
ฉันยังได้จัดทำวิธีการที่ทำสิ่งนี้ด้วย
def filternumeric(x):
if (re.search('\d+', str(x))): pass
else: return x
df.productid = df[df.productid.apply(filternumeric)].astype(int)
แต่มันก็ทำงานไม่ถูกต้องเช่นกัน มีข้อเสนอแนะที่ดีกว่านี้ไหม?
df[pd.to_numeric(df.productid, errors='coerce').notnull()]
- person cs95   schedule 05.08.2017