To remove commas from all the columns in a pandas DataFrame, you can use the apply()
method and pass a lambda function that removes the commas from each value in the column. Here is an example:
import pandas as pd
# Load the DataFrame
df = pd.read_csv("data.csv")
# Remove commas from all columns
df = df.apply(lambda x: x.str.replace(',',''))
This will remove commas from all columns in the DataFrame. If you only want to remove commas from specific columns, you can specify the column names in the apply()
method like this:
df[['col1', 'col2', 'col3']] = df[['col1', 'col2', 'col3']].apply(lambda x: x.str.replace(',',''))
This will remove commas from the col1
, col2
, and col3
columns. You can also use the applymap()
method to remove commas from every element in the DataFrame:
df = df.applymap(lambda x: x.replace(',',''))