Search code examples
pythonpandasdataframemissing-data

How can I drop rows based on given conditions in pandas and how to force converting data types?


I want to drop rows that don't verify a condition, I tried the code below, but it's not working

sample.drop(sample.loc[(sample.service == 'ftp') & (sample.is_ftp_login.isna())].index, inplace=False)

I also tried a loop, with condition isna() and with ' ', but it didn't work

for index, row in sample.iterrows():
    if row['service'] == 'ftp' and row['is_ftp_login'].isna():
        sample.drop([index])

I also want to change types from object to int and from float to int, (I tried both lines) it returns cannot convert to Class int.

sample['ct_ftp_cmd']=int(sample['ct_ftp_cmd'])
sample['ct_ftp_cmd']=str(int(sample['ct_ftp_cmd']))

Do you guys have any idea how to solve this, Thanks. I


Solution

  • Thank you, I solved the problem. I was able to convert the string type to numeric using this:

    sample['ct_ftp_cmd']= pd.to_numeric(sample['ct_ftp_cmd']) 
    

    and I dropped rows based on a given condition using this:

    sample = sample.drop(sample[(sample.service == 'ftp') & (sample.is_ftp_login.isna())].index)