1

I have a Pandas dataframe df with extraneous information. The extraneous information is stored in the columnns that have names containing "PM". I would like to remove these columns but I'm not sure how to. Below is my attempt to do this. However, I received this error message: AttributeError: 'numpy.float64' object has no attribute 'PM'. I'm not sure how to interpret this error message. I also don't understand why numpy is mentioned in the message since the dataframe df is a pandas object.

for j in range(0,len(df.columns)-1):
 df.iloc[0,j].str.contains("PM"):
   df.drop(j, axis=1)

AttributeError: 'numpy.float64' object has no attribute 'PM'

1
  • Can you add some small data sample, 3 columns, 3 rows? Commented Oct 19, 2021 at 5:53

3 Answers 3

1

Using an empty dataframe

df = pd.DataFrame(columns=['a','b','ABCPMYXZ','QWEPMQWE','c','d'])
df
df = df[[i for i in df.columns if not 'PM' in i]]
df

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Based on what I understood, you want to delete columns, so initially you should store all the column names in a list. Next remove all the elements from the list which doesn't contain PM in it.

columns = list(df.columns.values)
columns = [col for col in columns if 'PM' in col]
df.drop(columns=columns, axis=1, inplace=True)

2 Comments

Careful removing elements of a list while iterating over it, it often leads to incorrect results, in particular when two consecutive elements should be removed.
Yes, thanks for pointing it out.
0

Use regex with filter:

df.filter(regex='^((?!PM).)*$')

This is the shortest solution here.

1 Comment

@jezrael No time. Edited it out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.