0
私はパンダを使用してデータ科学の問題に取り組もうとしています。私のデータセットには、「国」、「変換」、「テスト」、「ユーザーID」などの列があります。国の列には、約10カ国があります。 「テスト」列は、0と1の2つのタイプのテストを示す値0と1を有する。「変換」はまた、人が変換したかどうかを示す値0と1を有する。TypeError: 'str'オブジェクトと 'float'オブジェクトを連結できません:pandas
私はcountryをgroupbyにして、すべてのグループに対してテストのp値と平均== 0とテスト== 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
完全なエラーメッセージ:df.dtypesの
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
が出力:
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
完全なスタックトレースを投稿する –
私は、あなたが 'float'と' string'値が混在した 'obj'型のカラムを持っていると思っています。 –
@ juanpa.arrivillaga:完全なエラーメッセージを投稿しました。 – Gingerbread