0

Data from json is in df and am trying to ouput to a csv. I am trying to multiply dataframe column with a fixed value and having issues how data is displayed

I have used the following but the data is still not how i want to display

df_entry['Hours'] = df_entry['Hours'].multiply(2)
df_entry['Hours'] = df_entry['Hours'] * 2

Input

ID, name,hrs
100,AB,37.5

Expected

ID, name,hrs
100,AB,75.0

What I am getting

ID, name,hrs
100,AB,37.537.5
cs95
  • 379,657
  • 97
  • 704
  • 746
Donnie
  • 1
  • 1
  • It appears "Hours" is a string column, so try converting it to numeric and multiplying using, `pd.to_numeric(df_entry['Hours'], errors='coerce') * 2` – cs95 May 31 '19 at 00:40

2 Answers2

2

That happens because the dtype of the column is str. You need to convert it to float before multiplication.

df_entry['Hours'] = df_entry['Hours'].astype(float) * 2
foxpal
  • 586
  • 4
  • 14
0

You can use apply function.

df_entry['Hours'] = df_entry['Hours'].apply(lambda x: float(int(x))*2)
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38