0

I scraped this table from this URL:

"https://www.patriotsoftware.com/blog/accounting/average-cost-living-by-state/"

Which looks like this:

 State  Annual Mean Wage (All Occupations)  Median Monthly Rent Value of a Dollar
    0   Alabama $44,930 $998    $1.15
    1   Alaska  $59,290 $1,748  $0.95
    2   Arizona $50,930 $1,356  $1.04
    3   Arkansas    $42,690 $953    $1.15
    4   California  $61,290 $2,518  $0.87

And then I wrote this function to help me turn the strings into ints:

def money_string_to_int(s):
return int(s.replace(",", "").replace("$",""))

money_string_to_int("$1,23")

My function works when I apply it to only one column. I found this answer here about using on multiple columns: How to apply a function to multiple columns in Pandas

But my code below does not work and produces no errors:

ls = ['Annual Mean Wage (All Occupations)', 'Median Monthly Rent',
       'Value of a Dollar']

ppe_table[ls] = ppe_table[ls].apply(money_string_to_int)
user2415706
  • 932
  • 1
  • 7
  • 19

1 Answers1

1

Lets try

df.set_index('State').apply(lambda x: (x.str.replace('[$,]','').astype(float))).reset_index()
wwnde
  • 26,119
  • 6
  • 18
  • 32
  • Ok thank you this works. I have another geodataframe that has some more wage data but it also has some co-ordinates in a column. How would I select a subset of those columns? Is it just df['a','b'].rest_of_your_code? – user2415706 Dec 09 '20 at 02:14
  • 1
    Are you able to add the coordinates columns to your sample df? – wwnde Dec 09 '20 at 02:17
  • 1
    Yeah, I can merge this cost of living data frame with the coordinates columns on a "State" column. I ended up just creating a temporary data frame without the coordinate stuff, using your function, and then re-merging that geo dataframe and then merging that with this cost of living dataframe. A little messy but whatever. Thank you so much! – user2415706 Dec 09 '20 at 02:33