亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何按行獲取熊貓數據框中不唯一的項目數?

如何按行獲取熊貓數據框中不唯一的項目數?

catspeake 2021-10-05 16:04:48
我發現的大多數解決方案是獲取數據框中唯一項目(我不想要)的數量,或者按具有明確標題的列進行計數。我的數據框如下所示:       1   2   3   4ILLU1 ATG --T --- TGGILLU2 ATG -CT GGG TGGILLU3 ATG TTT AAA TGGILLU4 -TG --T --- T-G我試圖獲得每行的計數,其中每列都有一個完整的 3 堿基序列。因此,如果單元格中存在“-”,它將為零(這包括“---”、“--N”、“-NN”)。所以我試圖使輸出如下:ILLU1 2ILLU2 3ILLU3 4ILLU4 0我試過了:df_new = pd.DataFrame() # to hold the final valuescount = 0for rows in df:   if not sum(df[rows].str.contains("-")) > 0: # if no hyphen present       count += 1 # add to final count   else:       count = count # does not get included final count       df_new["Final Count"] = count print(df_new)但我只是得到一個沒有值的空數據框。
查看完整描述

2 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

使用 str.contains


s=(~df.apply(lambda x : x.str.contains('-'))).sum(1)

s

Out[384]: 

ILLU1    2

ILLU2    3

ILLU3    4

ILLU4    0

dtype: int64


查看完整回答
反對 回復 2021-10-05
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

您可以使用:


df.apply(lambda x: ['-' not in i for i in x]).sum(1)

或者


df.apply(lambda x: ['-' in i for i in x]).ne(1).sum(1)

輸出:


ILLU1    2

ILLU2    3

ILLU3    4

ILLU4    0

dtype: int64


查看完整回答
反對 回復 2021-10-05
  • 2 回答
  • 0 關注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號