简体   繁体   中英

Why am I getting TypeError: unsupported operand type(s) for /: 'str' and 'int'?

Here in the below question on stats, I am trying a "two-sample independent t-test" in python.

An analyst at a department store wants to evaluate a recent credit card promotion. To this end, 500 cardholders were randomly selected. Half received an ad promoting a reduced interest rate on purchases made over the next three months, and half received a standard seasonal ad. Is the promotion effective to increase sales? Below is my code. I am doing some mistake writing the code please help.

from scipy import stats

std_promo = cust[(cust['insert'] == 'Standard')]
new_promo = cust[(cust['insert'] == 'New Promotion')]

print(std_promo.head(3))
print(new_promo.head(3))

     id    insert      dollars
0   148  Standard  2232.771979
2   973  Standard  2327.092181
3  1096  Standard  1280.030541

     id         insert      dollars
1   572  New Promotion  1403.807542
4  1541  New Promotion  1513.563200
5  1947  New Promotion  1729.627996

print (std_promo.mean())
print (new_promo.mean())

    id         69003.000000
    dollars     1566.389031
    dtype: float64
    id         64998.244000
    dollars     1637.499983
    dtype: float64
    print (std_promo.std())
    print (new_promo.std())
    id         37753.106923
    dollars      346.673047
    dtype: float64
    id         38508.218870
    dollars      356.703169
    dtype: float64

stats.ttest_ind(a= std_promo, b= new_promo, equal_var= True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-b40f7d9d7a3e> in <module>
      1 stats.ttest_ind(a= std_promo,
----> 2                 b= new_promo)

~\Anaconda3\lib\site-packages\scipy\stats\stats.py in ttest_ind(a, b, axis, equal_var, nan_policy)
   4163         return Ttest_indResult(np.nan, np.nan)
   4164 
-> 4165     v1 = np.var(a, axis, ddof=1)
   4166     v2 = np.var(b, axis, ddof=1)
   4167     n1 = a.shape[axis]

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in var(a, axis, dtype, out, ddof, keepdims)
   3365 
   3366     return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 3367                          **kwargs)
   3368 
   3369 

~\Anaconda3\lib\site-packages\numpy\core\_methods.py in _var(a, axis, dtype, out, ddof, keepdims)
    108     if isinstance(arrmean, mu.ndarray):
    109         arrmean = um.true_divide(
--> 110                 arrmean, rcount, out=arrmean, casting='unsafe', subok=False)
    111     else:
    112         arrmean = arrmean.dtype.type(arrmean / rcount)

TypeError: unsupported operand type(s) for /: 'str' and 'int'

I think you need to change:

stats.ttest_ind(a= std_promo, b= new_promo, equal_var= True)

to

stats.ttest_ind(a= std_promo.dollars, b= new_promo.dollars, equal_var= True)

I created a similar DF as yours, and ran this and it worked using dollars:

Ttest_indResult(statistic=7.144078895160622, pvalue=9.765848295636031e-05)

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