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))
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With