I have a df where there are 60 columns in total, and 4 categorical columns, I want to make a loop to check which are numerical columns. If they are not numeric I want to drop it. I have made the below loop but this is dropping only one of the categotcal columns, and the rest remain as is. Can you please tell me what I have done wrong and how i can improve my solution.
Thank you!
JavaScript
x
9
1
numeric = modified_df._get_numeric_data().columns
2
for x in modified_df.columns:
3
for y in numeric:
4
if x == y:
5
print(x)
6
else:
7
dropped_df = modified_df.drop('x', axis = 1)
8
dropped_df
9
Advertisement
Answer
You can use select_dtypes
:
JavaScript
1
2
1
df_new = df.select_dtypes(exclude='category')
2