TypeError:无法连接“ str”和“ float”对象:pandas

姜饼

我正在尝试使用熊猫解决数据科学问题。我的数据集包含以下列:“国家/地区”,“转化”,“测试”,“用户ID”等。在“国家/地区”列中,大约有10个国家/地区。“测试”列的值0和1表示两种类型的测试:控制0和实验1。“转化”也具有值0和1,表示此人是否已转化。

我想按国家/地区分组,并为每个组计算p值和test == 0和test == 1的平均值。我正在尝试使用以下函数,但是会引发错误“ TypeError:无法连接'str'和'float'对象”。有人可以阐明这一点吗?

def f(x):
        control = x.loc[(x.test==0)]
        test = x.loc[(x.test==1)]
        p_value = stats.ttest_ind(control,test)[0]
        control_mean = control['conversion'].mean()
        test_mean = test['conversion'].mean()
        return pd.Series({'p_value': p_value, 'conversion_test': test_mean, 'conversion_control': control_mean})     

bycountry = data1.groupby('country').apply(f) 
bycountry = bycountry.reset_index(level='None')
bycountry

完整的错误消息:

TypeError                                 Traceback (most recent call last)
<ipython-input-495-bd6227878520> in <module>()
      7     return pd.Series({'p_value': p_value, 'conversion_test': test_mean, 'conversion_control': control_mean})
      8 
----> 9 bycountry = data1.groupby("country").apply(f)
     10 bycountry = bycountry.reset_index(level='None')
     11 bycountry

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in apply(self, func, *args, **kwargs)
    649         # ignore SettingWithCopy here in case the user mutates
    650         with option_context('mode.chained_assignment', None):
--> 651             return self._python_apply_general(f)
    652 
    653     def _python_apply_general(self, f):

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in _python_apply_general(self, f)
    653     def _python_apply_general(self, f):
    654         keys, values, mutated = self.grouper.apply(f, self._selected_obj,
--> 655                                                    self.axis)
    656 
    657         return self._wrap_applied_output(

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in apply(self, f, data, axis)
   1525             # group might be modified
   1526             group_axes = _get_axes(group)
-> 1527             res = f(group)
   1528             if not _is_indexed_like(res, group_axes):
   1529                 mutated = True

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\pandas\core\groupby.pyc in f(g)
    645         @wraps(func)
    646         def f(g):
--> 647             return func(g, *args, **kwargs)
    648 
    649         # ignore SettingWithCopy here in case the user mutates

<ipython-input-495-bd6227878520> in f(x)
      2     control = x.loc[(x.test==0)]
      3     test = x.loc[(x.test==1)]
----> 4     p_value = stats.ttest_ind(control,test)[0]
      5     control_mean = control['conversion'].mean()
      6     test_mean = test['conversion'].mean()

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\scipy\stats\stats.pyc in ttest_ind(a, b, axis, equal_var, nan_policy)
   3865         return Ttest_indResult(np.nan, np.nan)
   3866 
-> 3867     v1 = np.var(a, axis, ddof=1)
   3868     v2 = np.var(b, axis, ddof=1)
   3869     n1 = a.shape[axis]

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\numpy\core\fromnumeric.pyc in var(a, axis, dtype, out, ddof, keepdims)
   3098 
   3099     return _methods._var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
-> 3100                          keepdims=keepdims)

C:\Users\SnehaPriya\Anaconda2\lib\site-packages\numpy\core\_methods.pyc in _var(a, axis, dtype, out, ddof, keepdims)
     89     # Note that if dtype is not of inexact type then arraymean will
     90     # not be either.
---> 91     arrmean = umr_sum(arr, axis, dtype, keepdims=True)
     92     if isinstance(arrmean, mu.ndarray):
     93         arrmean = um.true_divide(

TypeError: cannot concatenate 'str' and 'float' objects

df.dtypes的输出:

user_id                      int64
date                datetime64[ns]
source                      object
device                      object
browser_language            object
ads_channel                 object
browser                     object
conversion                   int64
test                         int64
sex                         object
age                        float64
country                     object
dtype: object
姜饼
def f(x):
    control = x.loc[(x.test==0)]
    control = control['conversion']
    test = x.loc[(x.test==1)]
    test = test['conversion']
    p_value = stats.ttest_ind(control,test)[0]
    control_mean = control.mean()
    test_mean = test.mean()
    return pd.Series({'p_value': p_value, 'conversion_test': test_mean, 'conversion_control': control_mean})     

这成功了!!再次感谢您@ juanpa.arrivillaga!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeError:无法连接“ str”和“ UUID”对象

TypeError:无法连接“ str”和“ Redditor”对象

Python:TypeError无法连接'str和'list'对象

Python Selenium TypeError:无法连接“ str”和“ WebElement”对象

TypeError:无法连接“ str”和“ pygame.Surface”对象

Python:TypeError:无法连接“ str”和“ int”对象

re.search()TypeError:无法连接“ str”和“ NoneType”对象

TypeError:无法连接'str'和'function'对象的python文件

Tkinter Label,TypeError:无法连接“ str”和“ instance”对象

abaqus脚本:TypeError:无法连接“ str”和“ Set”对象

Python TypeError:无法连接“ str”和“ list”对象

TypeError:无法使用MySQL连接Flask中的'str'和'float'对象

openerp无法连接“ str”和“ function”对象

TypeError:无法连接电子邮件中的“ str”和“ list”对象

TypeError:无法连接类型为“ <class'str'>”的对象;仅Series和DataFrame objs有效

Python TypeError:尝试打印时无法连接“ str”和“ int”对象

TypeError:无法连接'str'和'NoneType'对象python bs4

TypeError:无法将“ float”对象隐式转换为str或TypeError:-:“ str”和“ float”的不受支持的操作数类型

Python:TypeError:无法连接“str”和“int”

TypeError:无法连接“ str”

TypeError:将自定义网址放入scrapy.Request()时,无法连接“ str”和“ NoneType”对象

类型错误:无法连接“str”和“int”对象 1

rpsls游戏“(python)错误:无法连接'str'和'int'对象

类型错误无法连接“ str”和“ ErrorList”对象

无法连接'str'和'list'对象列表包含整数

如何连接str和int对象?

TypeError:无法连接类型为“<class 'str'>”的对象;只有 Series 和 DataFrame objs 有效我收到此错误

TypeError:-:'str'和'float'python pandas的不受支持的操作数类型

TypeError:无法将字节连接到str