Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to remove commas and dollars signs with Pandas in Python

Tags:

python

pandas

Tring to remove the commas and dollars signs from the columns. But when I do, the table prints them out and still has them in there. Is there a different way to remove the commans and dollars signs using a pandas function. I was unuable to find anything in the API Docs or maybe i was looking in the wrong place

 import pandas as pd     import pandas_datareader.data as web  players = pd.read_html('http://www.usatoday.com/sports/mlb/salaries/2013/player/p/')   df1 = pd.DataFrame(players[0])   df1.drop(df1.columns[[0,3,4, 5, 6]], axis=1, inplace=True) df1.columns = ['Player', 'Team', 'Avg_Annual'] df1['Avg_Annual'] = df1['Avg_Annual'].replace(',', '')  print (df1.head(10)) 
like image 961
Mark Avatar asked Jul 22 '16 00:07

Mark


People also ask

How do you ignore dollar signs in Python?

replace('$','') method on the entire column. This is the most straightforward method, as it simply replaces the '$' with a blank space for each item in the column.

How do I remove commas from numbers in pandas?

sub() function to erase commas from the python string. The function re. sub() is used to swap the substring. Also, it will replace any match with the other parameter, in this case, the null string, eliminating all commas from the string.

How do I remove special characters from a DataFrame in Python?

Add df = df. astype(float) after the replace and you've got it. I'd skip inplace and just do df = df. replace('\*', '', regex=True).

How do I get rid of the sign in Python?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.


Video Answer


1 Answers

You have to access the str attribute per http://pandas.pydata.org/pandas-docs/stable/text.html

df1['Avg_Annual'] = df1['Avg_Annual'].str.replace(',', '') df1['Avg_Annual'] = df1['Avg_Annual'].str.replace('$', '') df1['Avg_Annual'] = df1['Avg_Annual'].astype(int) 

alternately;

df1['Avg_Annual'] = df1['Avg_Annual'].str.replace(',', '').str.replace('$', '').astype(int) 

if you want to prioritize time spent typing over readability.

like image 169
mechanical_meat Avatar answered Sep 22 '22 10:09

mechanical_meat