0

I need to remove all columns whose values are not numbers.

I tried to look at the CSV and type the names of the columns I wanted to be removed. I am wondering if there is an easier way to do this.

M Z
  • 4,571
  • 2
  • 13
  • 27
  • 2
    Could you provide example code and explain what have you tried ? – KSs May 01 '23 at 17:39
  • This might be helpful for you (tip: use 'exclude' arg instead of 'include'): https://stackoverflow.com/a/28155580/19533532 – Masha May 01 '23 at 21:46

1 Answers1

0

First, load your CSV file into a Pandas DataFrame, and then filter the columns based on their data types. Here's a code snippet to demonstrate this:

import pandas as pd

# Load your CSV file
df = pd.read_csv("your_file.csv")

# Select only numeric columns
numeric_columns = df.select_dtypes(include=[np.number])

# Save the result to a new CSV file
numeric_columns.to_csv("filtered_file.csv", index=False)

The select_dtypes function is used to select only columns with numeric data types. The resulting DataFrame with only numeric columns is then saved to a new CSV file.

Taras Drapalyuk
  • 473
  • 3
  • 6