将熊猫系列更改为int

时间:2019-03-27 02:35:37

标签: 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是熊猫系列。我尝试使用x1.astype(int),但似乎无法解决问题。

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

1 个答案:

答案 0 :(得分:2)

您要.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])