Python Forum
(Solved) Converting Dollar Amount Str to Int with Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(Solved) Converting Dollar Amount Str to Int with Pandas
#2
Changed the title to be more specific. I have an update if anyone else stumbles across this searching for an answer.

First I'll detail better what is still NOT working:

def fix(x):
x.replace('€', '')
if 'M' in x:
x.replace('M', '')
return x
if 'K' in x:
x.replace('K', '')
return x

df1 = pd.DataFrame(df, columns=['Name', 'Value', 'Wage'])
df1['Wage'] = df1['Wage'].apply(fix)
df1['Value'] = df1['Value'].apply(fix)
df1.head(6)

Here I have not added the conversion because this is not even working on its own. The head() command works and things are printed, but without the characters being replaced. However, if I write the same code with a lambda function, it works. Pandas is really annoying me Think

Now for what DOES work:

df1['Wage'] = df1['Wage'].replace('[€MK]', '', regex=True).astype(float)*1000
df1['Value'] = df1['Value'].replace('[€MK]', '', regex=True).astype(float)*1000000

I guess that is nicer to get it all done in two lines, and here we have some Pandas specific methods, which is nice to know, but I still don't understand what happened with the other variation.

If someone could explain why one of these works and the other does not it would be very helpful to my understanding and sanity Smile
Reply


Messages In This Thread
RE: Converting Dollar Amount Str to Int with Pandas - by calvinsomething - Nov-19-2020, 01:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] duplication in pandas BSDevo 0 591 Sep-06-2023, 10:47 PM
Last Post: BSDevo
  Pandas converting date to epoch randor 2 4,012 Jul-16-2019, 02:41 AM
Last Post: scidam
  Converting string the pandas dataframe chrismc 0 2,382 Jan-24-2019, 11:07 AM
Last Post: chrismc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020