Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何识别指定列范围内的某些行?_Python_Pandas - Fatal编程技术网

Python 如何识别指定列范围内的某些行?

Python 如何识别指定列范围内的某些行?,python,pandas,Python,Pandas,我有一个df,其中我需要识别在列表中有值的任何行,在另一个列表中的列中。 对于本例,我需要在以“月”开头的任何列中标识值为J、Q、R的任何行。 如果列列表中存在任何一个字母,则最终df将有一列显示true或false df = pd.DataFrame({'KEY': ['1312', '1345', '5555', '5555','5555'], 'Month1': [1, 1, 1,1,1], 'Month2': [1, 1, 'J'

我有一个df,其中我需要识别在列表中有值的任何行,在另一个列表中的列中。
对于本例,我需要在以“月”开头的任何列中标识值为J、Q、R的任何行。
如果列列表中存在任何一个字母,则最终df将有一列显示true或false

df = pd.DataFrame({'KEY': ['1312', '1345', '5555', '5555','5555'], 
              'Month1': [1, 1, 1,1,1],
              'Month2': [1, 1, 'J',1,1],
              'Month3': [1, 1, 1,1,1],
              'Month4': [1, 'J', 1,1,1],
              'Month5': [1, 1, 1,0,0],
              'Month6': [1, 1, 1,0,0],
              'Date1': [20120304, 20120102, 20120203,20120402,4],
              'Date2': [20120405,20120104,20120502,20120501,4],
              'StartMonth': [3,1,1,4,3],
              'EndMonth': [4,1,3,5,5],
              'ID': [1,2,3,3,4]})

df[['KEY','ID','Date1','Date2','StartMonth','EndMonth','Month1', 'Month2','Month3','Month4','Month5','Month6']]
预期成果:

    Date1       Date2       EndMonth    ID  KEY     Month1  Month2  Month3  Month4  Month5  Month6  StartMonth  HemoFacB
0   20120304    20120405    4           1   1312    1       1       1       1       1       1       3           False
1   20120102    20120104    1           2   1345    1       1       1       J       1       1       1           True
2   20120203    20120502    3           3   5555    1       J       1       1       1       1       1           True
3   20120402    20120501    5           3   5555    1       1       1       1       0       0       4           False
4   4           4           5           4   5555    1       1       1       1       0       0       3           False
我最初的尝试导致以下错误:

codes = ['J','Q','R']
cols = [col for col in df if col.startswith(('Month'))]
df['HemoFacB'] = np.where(df[cols].isin(codes),1,0)

ValueError: Wrong number of items passed 6, placement implies 1
我忘了添加
.any()

下面的代码可以工作

df['HemoFacB'] = np.where(df[cols].isin(codes),1,0).any(1)

错误表明我试图将太多(6列)项比较成一个结果。通过使用,如果任何iterables(cols)=“True”,则此函数返回“True”;如果iterable返回所有“false”,则此函数返回false,最终将项目数减少为1。因此,通过在末尾添加
.any(1)
,脚本将传递的6项合并为一项。

这里有一个不使用numpy的解决方案。我没有使用所有字段,但我相信您会理解的。另外,我上次操作字典后使用了一个数据帧。我发现这样做容易多了

import pandas as pd

mydict = {'KEY': ['1312', '1345', '5555', '5555','5555'], 'Month1': [1, 'J', 3,4,'J']}

#print(df)

truth_list = []
for val in zip(*mydict.values()):
    #print(val)
    #print("This is key: {} and value: {}".format(key, val))
    if 'J' in val:
        #print("True")
       truth_list.append('True')
    else:
        #print("False")
        truth_list.append('False')
    #print("Row {}".format(row = row + 1))

mydict.update({'HemoFacB': truth_list})

df = pd.DataFrame(mydict)
print(df)

马蒂,我想这是个不错的问题。您应该添加第一次尝试失败的原因以及解决方案返回的预期输出。StackOverflow是问答网站,而不是讨论论坛。请提出一个问题,否则就不清楚读者的答案是什么。