0

I am reading bunch of files as a data frame. Some of the columns have values stored at $1 ,$0 etc . I want to remove all the dollar signs (columns name and number having $ values will vary from file to file). What will be the most efficient way to do this Bellow is an example of a sample df

I was thinking of using iterrows. for row in df.iterrows():

Then may be check each value in row and see if it starts with $ then change the trim $ and store it back in the data frame.

Not sure how to proceed with that approach . Any help is apricated

Data Frame example

Moinak
  • 115
  • 1
  • 1
  • 6

2 Answers2

1

I think you should have added the way you generate this code, at least the part where you get these variables. I would comment this but I don't have enough points.

Wouldn't this work?

for i in range(len(char)):
char[i] = char[i].replace('$', '')

The replace function will change the string value of $ to the second one which is what you need. This might be wrong though since I don't know how you read the data.

Edit: For pandas,pandas.replace() might be the solution.

df['beginning'] = df['beginning'].replace({'$':''})
Batselot
  • 194
  • 10
  • Thanks for your reply . I am looking to do this using pandas df. rather than writing 2 loops and iterating though each value and changing it – Moinak Apr 26 '21 at 22:48
  • I have tried one more and edited the answer for pandas solution, would you check again? – Batselot Apr 26 '21 at 22:56
  • Thanks again . The problem with your approach is that the number of columns that start with $is variable . So won't be able specify the names like df['beginning'] = df['beginning'].replace({'$':''}) – Moinak Apr 26 '21 at 23:07
  • I see, you can check the whole variable with an if condition to see if any dolar signs exist and you can use the replace function. However, this might be a bit complicated for your case. Since you might have multiple variables. – Batselot Apr 26 '21 at 23:10
0

You only need to replace a character, right? This should work:

for feature in df.columns:
    df[feature] = df[feature].map(str).str.replace('$','').str.strip()
GabrielP
  • 777
  • 4
  • 8
  • found a faster way using list comprehension if anyone runs into this issue :- for col in df.columns: df[col] = [x[1:] for x in df[col]] – Moinak Apr 29 '21 at 13:47