Pandas remove thousands separator

Removal of thousands separator in pandas

To remove thousands separator in pandas, you can make use of the .str.replace() method in combination with regular expressions or string manipulation.

Example:

Suppose you have a DataFrame with a column containing numbers represented as strings with thousands separators:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'Numbers': ['1,000', '2,500', '100,000']})

# Display the initial DataFrame
print(df)

The initial DataFrame will look like this:

Numbers
0 1,000
1 2,500
2 100,000

To remove the thousands separator comma, you can use the .str.replace() method with a regular expression:

# Remove the comma
df['Numbers'] = df['Numbers'].str.replace(',', '')

# Display the modified DataFrame
print(df)

The modified DataFrame will look like this:

Numbers
0 1000
1 2500
2 100000

Now, the thousands separator comma has been removed from the numbers in the 'Numbers' column.

Leave a comment