简体   繁体   中英

Pandas multiply the value in one column with another if the column vaues are numbers?

I am not able to understand how to correct this

c_df

   lotsize strike_lots amt_cvr
0  75.0     1.0         1.0   
1  2500.0   N           943845
2  100.0    N           742350
3  600.0    2.0         2.0   
4  8000.0   N           585214
5  3500.0   N           838704
6  6000.0   2.0         2.0   
7  4000.0   N           709020
8  1500.0   N           263610

c_df.loc[c_df['strike_lots'] != 'N', 'amt_cvr'] = float(c_df['strike_lots'])*c_df['lotsize']

I have checked dtypes

lotsize        float64
strike_lots    object 
amt_cvr        object 
dtype: object

The problem I assume is the dtype of strike_lots which I don't want to change as I want to keep N values.

Traceback (most recent call last):
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1012, in na_op
    result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 205, in evaluate
    return _evaluate(op, op_str, a, b, **eval_kwargs)
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/computation/expressions.py", line 65, in _evaluate_standard
    return op(a, b)
TypeError: can't multiply sequence by non-int of type 'float'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1033, in safe_na_op
    return na_op(lvalues, rvalues)
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1018, in na_op
    result[mask] = op(x[mask], com._values_from_object(y[mask]))
TypeError: can't multiply sequence by non-int of type 'float'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1069, in wrapper
    result = safe_na_op(lvalues, rvalues)
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1037, in safe_na_op
    lambda x: op(x, rvalues))
  File "pandas/_libs/algos_common_helper.pxi", line 1212, in pandas._libs.algos.arrmap_object
  File "/media/sid1/sid/lib/python3.6/site-packages/pandas/core/ops.py", line 1037, in <lambda>
    lambda x: op(x, rvalues))
TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')

Expected output

c_df

   lotsize strike_lots amt_cvr
0  75.0     1.0         75.0    #this value changes  
1  2500.0   N           943845
2  100.0    N           742350
3  600.0    2.0         1200.0   #This value changes   
4  8000.0   N           585214
5  3500.0   N           838704
6  6000.0   2.0         12000.0 #This value changes  
7  4000.0   N           709020
8  1500.0   N           263610

Thanks in advance.

You can use to_numeric and fillna

df['amt_cvr'] = (df['lotsize']*pd.to_numeric(df['strike_lots'],errors='coerce')).fillna(df['amt_cvr'])

   lotsize strike_lots   amt_cvr
0     75.0         1.0      75.0
1   2500.0           N  943845.0
2    100.0           N  742350.0
3    600.0         2.0    1200.0
4   8000.0           N  585214.0
5   3500.0           N  838704.0
6   6000.0         2.0   12000.0
7   4000.0           N  709020.0
8   1500.0           N  263610.0

pd.to_numeric(df['strike_lots'],errors='coerce') will convert the non numeric values to NaN s. So when you multiply it with the numeric column the output of will also be a NaN .

We can then use fillna to fill the null values with the actual amt_cvr . Hope it helps.

By using a mask , and this is for correct your own one line code

mask=df['strike_lots'] != 'N'
df.loc[mask, 'amt_cvr'] = df.loc[mask,'strike_lots'].astype(float)*df['lotsize']
df
Out[80]: 
   lotsize strike_lots   amt_cvr
0     75.0         1.0      75.0
1   2500.0           N  943845.0
2    100.0           N  742350.0
3    600.0         2.0    1200.0
4   8000.0           N  585214.0
5   3500.0           N  838704.0
6   6000.0         2.0   12000.0
7   4000.0           N  709020.0
8   1500.0           N  263610.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM