python - 将 panda 系列更改为 int

标签 python pandas

我有以下代码:

x1 = df[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL')].yes_user_count
y1 = df[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL')].yes_user_count

x_n = df[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL')].total_user_count
y_n = df[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL')].total_user_count

zscore, pval = proportions_ztest([x1, y1], [x_n, y_n])

但是,ztest功能不起作用。原因看起来是 x1、y1、x_n 和 y_n 是 panda 系列。我尝试使用 x1.astype(int) 但它似乎没有解决问题。

有人可以建议什么是最好的解决方案吗?

最佳答案

你想要.tolist():

x1 = df[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL')].yes_user_count.tolist()
y1 = df[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL')].yes_user_count.tolist()

x_n = df[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL')].total_user_count.tolist()
y_n = df[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL')].total_user_count.tolist()

zscore, pval = proportions_ztest([x1, y1], [x_n, y_n])

但我也建议使用 loc 并将列名称作为第二个值:

x1 = df.loc[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL'), 'yes_user_count'].tolist()
y1 = df.loc[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL'), 'yes_user_count'].tolist()

x_n = df.loc[(df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL'), 'total_user_count'].tolist()
y_n = df.loc[(df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL'), 'total_user_count'].tolist()

zscore, pval = proportions_ztest([x1, y1], [x_n, y_n])

或者更好:

mask1 = (df['event_name'] == 'Listen') & (df['cell'] == 'CONTROL')
mask2 = (df['event_name'] == 'Listen') & (df['cell'] != 'CONTROL')
x1 = df.loc[mask1, 'yes_user_count'].tolist()
y1 = df.loc[mask2, 'yes_user_count'].tolist()

x_n = df.loc[mask1, 'total_user_count'].tolist()
y_n = df.loc[mask2, 'total_user_count'].tolist()

zscore, pval = proportions_ztest([x1, y1], [x_n, y_n])

关于python - 将 panda 系列更改为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55368946/

相关文章:

python - 如何使用 pandas 从目录中的 Excel 工作表中获取每一行值

python - 连接数据帧单标签行选择返回多行

python - 在 python 中打印混淆矩阵的精度

python - 使用 Python (Pandas) 的新计算列的性能影响

python - Pandas 中的groupby对不同的列具有不同的功能

python - 按前 N% 对 pandas 数据进行分箱

python - 返回 pandas 数据框中特定值的列名

python - 元组到 DataFrame 转换的列表

python - Pandas Dataframe 的切换轴

python - add_header 期望 3 个参数而不只是键/值