Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我在哪里出错得到这个错误:TypeError:只能将str(而不是“int”连接到str_Python_Pandas_Statsmodels - Fatal编程技术网

Python 我在哪里出错得到这个错误:TypeError:只能将str(而不是“int”连接到str

Python 我在哪里出错得到这个错误:TypeError:只能将str(而不是“int”连接到str,python,pandas,statsmodels,Python,Pandas,Statsmodels,这是我的代码,当它运行时,我得到这个错误TypeError:只能将str(而不是“int”)连接到str import pandas as pd import statsmodels.api as sm pd.set_option('display.max_columns', 30) pd.set_option('display.width', 1000) df = pd.read_csv('survey.csv') df['treatment'].replace({'Yes': 1,

这是我的代码,当它运行时,我得到这个错误
TypeError:只能将str(而不是“int”)连接到str

import pandas as pd
import statsmodels.api as sm

pd.set_option('display.max_columns', 30)

pd.set_option('display.width', 1000)

df = pd.read_csv('survey.csv')

df['treatment'].replace({'Yes': 1, 'No': '0'}, inplace=True)

df['family_history'].replace({'Yes': 1, 'No': '0'}, inplace=True)

ztest, pval = sm.stats.ztest(df['family_history'], df['treatment'],value=0)

引发错误的原因似乎是
df['treatment']
df['family\u history']
属于
str
类型,并且您正在用
int
值替换。试试这个:

df['treatment'].replace({'Yes': '1', 'No': '0'}, inplace=True)
df['family_history'].replace({'Yes': '1', 'No': '0'}, inplace=True)
如果替换后需要,还可以将列值转换为
int

df['treatment'] = df['treatment'].astype(int)
df['family_history'] = df['family_history'].astype(int)

添加描述和堆栈跟踪。阅读并更正代码以演示当前问题。如果一个类似的问题还没有发布,一定要先检查。你要连接到哪里?请显示失败的命令。请尝试
{'Yes':1,'No':0}
而不是
'0'
@KM\u 83:是否应该反过来<代码>{'Yes':'1','No':'0'}?