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.
- Python tkinter title bar color
- Pyarrow.lib.arrowtypeerror: expected a string or bytes dtype, got float64
- Property ‘timer’ does not exist on type ‘typeof observable’
- Pypdf2.errors.dependencyerror: pycryptodome is required for aes algorithm
- Property ‘component’ does not exist on type ‘intrinsicattributes
- Pandas read_csv multiple header rows
- Pandas dataframe to string with separator
- Pandas read_sql slow