0

I was having some trouble with csv files. I would like to add a new column, that being a multiplication of the previous one. For instance ['price', 'pricex2'] [[2,4],[6,12]] and so on, how could I do that?

I also happen to have € signs at the end of every number in the price column, but I think I can tackle that by taking the float for the 4 first characters right? For example price = 2,45€ numericalPrice = float(price[0:3]).

I would rather not use pandas as it gives me trouble when installing.

7
  • 1
    Are you using another package other than pandas? Commented Dec 31, 2021 at 11:48
  • Can't you just multiply it by two when you use the file? Storing 'this number multiplied by a constant' doesn't seem to add any information... Commented Dec 31, 2021 at 11:48
  • What part of doing the CSV transformation are you struggling with? Do you know how to read a CSV file, write to a CSV file? etc.? I think once you've tried to do that you should come back with your code if it doesn't work and ask a specific question. Commented Dec 31, 2021 at 11:51
  • As I'm a newbie and never used panda, I have this code writen down modified from kite.com/python/answers/… Commented Dec 31, 2021 at 11:53
  • def priceConverter(file,multiplyer): df = pd.read_csv(csvfile) df["Modified price"] = float(df["Jewerly_name_price"])[0:3]*multiplyer df.to_csv(csvfile, index=False) priceConverter(csvfile,2) Commented Dec 31, 2021 at 11:54

1 Answer 1

1

I'll answer the second part of the question, because your suggestion is dangerous. If the price is not exactly 4 characters long it fails. You should do:

price = "2,45€"
numericalprice = float(price[:-1])
print(numericalprice)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.