Mizanur Choudhury
Mizanur Choudhury

Reputation: 346

How to drop all strings in a column using a wildcard?

I have some data that changes regularly but the column headers need to be consistent (so I cant drop the headers) but I need to clear our the strings in a given column.

This is what I have now but this only seems to work for where I know what the string is called and one at a time?

    df1= pd.read_csv(r'C:\Users\Test.csv')
df2 = df1.drop(df1[~(df1['column'] != 'String1')].index)

Upvotes: 1

Views: 200

Answers (1)

ql.user2511
ql.user2511

Reputation: 389

You can use the pd.drop function which removes rows having a specific index from a dataframe.

for i in df.index:
    if type(df.loc[i, 'Aborted Reason']) ==  str:
        df.drop(i, inplace = True)

df.drop will remove the index having a string in the relevant column from the dataframe.

Upvotes: 2

Related Questions