我想在python datafram中搜索sting列表

2024-09-28 17:28:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我的CSV文件中有一列,我想在其中搜索字符串列表并添加一个0/1的新列,如果列表中存在任何值,则1或0。你知道吗

我有两张单子:

  1. UC、iCD、慢性病、慢性病、IBD、溃疡性 结肠炎、PMC、P80、慢性病
  2. 供者,健康,非IBD,对照组。你知道吗

我的专栏也有NA值

到目前为止,我有一个,我只是试图匹配列表的刺:

import csv
import pandas as pd

with open('biosample.csv') as csvfile:
    df = pd.read_csv('biosample.csv', delimiter = ',', dtype= 'unicode', 
    error_bad_lines=False)
    df1 = df.set_index(['Sample_Info'])
print(df1.loc['UC''iCD', 'Chrons disease', 'Chrons', 'IBD', 'Ulcerative 
colitis', 'PMC', 'P80', 'Chron disease])

对此,我得到了多个错误,如in\u has\u valid\u type\u error,in has\u valid\u type\u error。你知道吗

我已经通过已经张贴的问题,但没有这种错误被提及。你知道吗


Tags: csvimportdf列表aserrorpddf1
2条回答

演示:

In [84]: df
Out[84]:
   a   b    c    new
0  1  11  aaa   True
1  2  22  bbb  False
2  3  33  ccc   True
3  4  44  ddd  False

In [85]: lst = ['aaa','ccc','xxx']

In [86]: df['new'] = df['c'].isin(lst).astype(np.int8)

In [87]: df
Out[87]:
   a   b    c  new
0  1  11  aaa    1
1  2  22  bbb    0
2  3  33  ccc    1
3  4  44  ddd    0

PS您根本不需要使用CSV模块:

df = pd.read_csv(r'/path/to/biosample.csv', delimiter = ',', 
                 encoding='unicode', error_bad_lines=False, 
                 index_col='Sample_Info')

从csv文件加载数据帧时,不需要使用csv模块。你知道吗

正如您所提到的,新列应该添加到dataframe。你知道吗

用于检查来自第一个列表的值的代码可能如下所示:

import pandas as pd

list1 = ['UC''iCD', 'Chrons disease', 'Chrons', 'IBD', 'Ulcerative colitis', 'PMC', 'P80', 'Chron disease']
list2 = ['Donor', 'healthy', 'non-IBD', 'Control']

def check_list(value, list2check):
    if any(map(lambda x: x in value, list2check))
        return 1
    return 0

df = pd.read_csv('biosample.csv', delimiter = ',', dtype= 'unicode', error_bad_lines=False)
df['sample_from_list1'] = df['Sample_Info'].apply(lambda v: check_list(v, list1))

相关问题 更多 >