1

I have a DataFrame column named "price" that contains prices, which are either in 'pw' or 'pcm' format ('600pcm' for an example).

prices = {'price': ["100pw","600pcm","321pw"]}

df = DataFrame(prices,columns=['price'])

df.head()

I want to be able to convert all prices into weekly prices.

Ideally, there would be a loop function that would convert pcm amount into pw but I would have to remove the string part before I could convert the int.

I tried making a new list of prices in weeks and months through list comprehensions but how would I match them up to the correct row index?

Also tried the np.where function but it seems inappropriate.

Any thought guys? I am newbie to python

Meruem
  • 33
  • 4
  • Welcome to stack overflow! Unfortunately your description is not clear. Please [edit] to include a [mcve] , including samples of your input, samples of your desired output, and _code_ for what you've tried so far. See also: [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Mar 20 '20 at 22:13

1 Answers1

0
def pcm_to_pw(x):
  return(3.14 * x)

convert_ = lambda x: pcm_to_pw(float(x[:-3])) if x.endswith('pcm') else float(x[:-2])

df['price'] = df['price'].apply(convert_)

Of course, you need to adapt the pcm_to_pw function as I don't know the conversion rule.

Mrml91
  • 456
  • 3
  • 5