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'}?