将0或1的实例计数到系列

时间:2017-06-09 09:51:02

标签: python pandas dataframe series

如果我的数据框使用了一个我希望计算值为0或1的实例的列,那么迭代标记0或1的列对索引的语法是什么。

这样:

output = df.Series([0,1], index= ['no', 'yes'])

将返回:

no    0
yes       1
dtype: int64

而我希望整个列的整体no / yes标记为0或1。

实际数据框与scikit-learn数据相关,我在数据集的末尾创建了一个目标列:worst concave points worst symmetry worst fractal dimension target
0 0.26540 0.4601 0.11890 0.0

试图像这样映射:

    status = {0:'Malignant', 1:'Benign'}
    cancerdf['target'] = cancerdf['target'].map(status)

导致

TypeError: tuple indices must be integers or slices, not str

我试图返回一个系列,但似乎正在偏离轨道。

1 个答案:

答案 0 :(得分:2)

我认为<form method="post" class=" clearfix no-mar payment-form" action="checkout.php" id="checkoutRevisionProcess"> <input type="hidden" name="joborderno" id="postsecondrevieworderno" value="%%GLOBAL_postsecondrevisionorderno%%"/> <div class="clearfix action-btn-submit"> <div class="pull-right"> <a title="Yes" class="btn btn-primary postsecondrevisionyes">PAY</a> </div> </div> </form> rename

需要value_counts
map

或者:

np.random.seed(123)
s = pd.Series(np.random.choice([0,1], size=10))
print (s)
0    0
1    1
2    0
3    0
4    0
5    0
6    0
7    1
8    1
9    0
dtype: int32

d = {0:'No', 1:'yes'}
print (s.value_counts().rename(index=d))
No     7
yes    3
dtype: int64

或者可能需要map

d = {0:'No', 1:'yes'}
print (s.map(d).value_counts())
No     7
yes    3
dtype: int64

编辑:

我认为np.random.seed(123) df = pd.DataFrame({'A':np.random.choice([0,1], size=10)}) d = {0:'No', 1:'yes'} df['A'] = df['A'].map(d) print (df) A 0 No 1 yes 2 No 3 No 4 No 5 No 6 No 7 yes 8 yes 9 No 列中的数据type不是target,而是int

所以需要:

float

如果它不起作用,则有一些数据不是数字,解决方案是使用to_numeric将它们替换为status = {0:'Malignant', 1:'Benign'} cancerdf['target'] = cancerdf['target'].astype(int).map(status) ,然后将它们转换为某些NaN,如{{1}最后转换为int

2