简体   繁体   English

如何使用 python 和 Pandas 检查行是否在特定列名处包含 1

[英]How do I check to see if a row contains a 1 at a specific column name using python with Pandas

I want to print out C and C1 along with other row names that have 1 in the braf column.我想打印出 C 和 C1 以及 braf 列中具有 1 的其他行名称。 So I want my output to be C, C1,...所以我希望我的 output 是 C,C1,...

      'Braf'
'C'      1     
'NC'     0
'C1'     1
...      ...

If need index values if match column Braf use:如果匹配列Braf需要索引值,请使用:

m = df['Braf'] == 1
groupA, groupbB = df.index[m].tolist(), df.index[~m].tolist()

If need get values of column 0 :如果需要获取第0列的值:

m = df['Braf'] == 1
groupA, groupbB = df.loc[m, 0].tolist(), df.loc[~m, 0].tolist()

If need extract values by first column:如果需要按第一列提取值:

m = (df['Braf'] == 1).to_numpy()
groupA, groupbB = df.iloc[m, 0].tolist(), df.iloc[~m, 0].tolist()

EDIT:编辑:

df = pd.DataFrame({'col':['a','b','c'],
                   'Braf':[1,0, 1]}, index=['C','NC','C1'])
print (df)
   col  Braf
C    a     1
NC   b     0
C1   c     1

#labels of indices
print (df.index)
Index(['C', 'NC', 'C1'], dtype='object')

#first column - selected by position - labels are same
print (df.iloc[:, 0])
C     a
NC    b
C1    c
Name: col, dtype: object

#column Braf - labels are same
print (df['Braf'])
C     1
NC    0
C1    1
Name: Braf, dtype: int64

If you want the indices of rows with a specific value in a specific column you can use groupby :如果您想要在特定列中具有特定值的行的索引,您可以使用groupby

df = pd.DataFrame({'BRAF': [0,1,0,1,1]})

d = df.index.groupby(df['BRAF'].eq(1))
# you can also directly use 0/1 if binary

groupA = d[True]
# [0, 2]
groupB = d[False]
# [1, 3, 4]

Or directly as dictionary:或者直接作为字典:

out = df.index.groupby(np.where(df['BRAF'].eq(1), 'groupA', 'groupB'))

Output: Output:

{'groupA': [1, 3, 4],
 'groupB': [0, 2]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当特定列包含向我发出信号应删除该行的值时,如何删除Pandas数据框中的行? - How do I delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted? 如何检查 dataframe 列是否包含来自另一个 dataframe 列的字符串并返回 python Z3A0524F883225EFFA94 中的相邻单元格 - How do I check if dataframe column contains a string from another dataframe column and return adjacent cell in python pandas? 如何使用熊猫创建一个新列的最大值(对应于特定名称)? - How do I create a new column of max values of a column(corresponding to specific name) using pandas? 如何为 pandas 数据库中的特定行和列分配值? - How do I assign a value to a specific row and column in a pandas database? 如何从 pandas 中的列名中删除第二行? - How do I remove second row from the column name in pandas? 如何获取连续熊猫中具有最高值的列的名称 - How do I get the name of the column with the highest value in a row pandas 如何使用 Python Pandas 从 1 行中的特定列获取值? - How to get value from specific column in 1 row using Python Pandas? 如何使用计算值在 pandas 中创建新列并为每一行分配特定值? - How do I create a new column in pandas using calculated values and assign specific values to each row? Python,Pandas:如何检查一行是否包含另一行中找到的值? - Python, Pandas: How to check if a row contains values found in another row? 我如何使用 python pandas 数据框并使用列名和行名作为新列创建一个新表 - how do i take a python pandas dataframe and create a new table using the column and row names as the new column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM