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.
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.
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.