在数据帧中查找字符串,并将新值存储在新列中



我正在创建一个脚本,该脚本接受一个csv文件,该文件的列组织和列名未知。但是,我知道只有一列包含一些值,其中出现str‘rs’和del‘。

我需要创建一个额外的列(称为"Type"(,并在找到"rs"的行中存储"dbsnp",在找到"del"的行存储"delete"。如果找不到str,请将列类型中的此行保留为空。

作为例子,我提供了这个df:

Data = {'Number': ['Mukul', 'Rohan', 'Mayank', 
'Shubham', 'Aakash'], 

'Location': ['Saharsanpur', 'MERrs', 'rsAdela', 
'aaaadelaa', 'aaa'], 

'Pay': [25000, 30000, 35000, 40000, 45000]} 

df = pd.DataFrame(Data)
print(df)
Name     Location    Pay
0    Mukul  Saharsanpur  25000
1    Rohan        MERrs  30000
2   Mayank      rsAdela  35000
3  Shubham    aaaadelaa  40000
4   Aakash          aaa  45000

我一直在尝试类似的东西

df["type"] = df["Name"].str.extract("rs")[0] 
# and then do some replace

但我的一个问题是,我不知道专栏的名字,也不知道职位。

期望输出

Name     Location    Pay       type
0    Mukul  Saharsanpur  25000 dbsnp
1    Rohan        MERrs  30000 dbsnp
2   Mayank      rsAdela  35000 dbsnp
3  Shubham    aaaadelaa  40000 deletion
4   Aakash          aaa  450

下一个for循环解决了未知列的问题,但现在我需要解决在值中标识str的问题。

如何在if条件中使用str.contains("rs"(?

for index, row in df[:3].iterrows():
for i in range(len(df.columns)): 
if row[i] == 5:
print(row.index[i])

您可以在没有循环的情况下完成此操作。这里有一个方法。您可以使用applymap搜索所有列。

import pandas as pd
data = {'Number': ['Mukul', 'Rohan', 'Mayank', 
'Shubham', 'Aakash'], 

'Location': ['Saharsanpur', 'MERrs', 'rsAdela', 
'aaaadelaa', 'aaa'], 

'Pay': [25000, 30000, 35000, 40000, 45000]} 

df = pd.DataFrame(data)
df['rs'] = df.astype(str).applymap(lambda x: 'rs' in x).any(1)
df['del'] = df.astype(str).applymap(lambda x: 'del' in x).any(1)
df['type']=''
df.loc[df['rs'] == True, 'type'] = 'dbsnp'
df.loc[df['del'] == True, 'type'] = 'deletion'
df = df.drop(columns=['rs','del'])
print (df)

根据表中的数据,rsAdela同时具有rsdel。由于我首先应用rs,其次应用del,所以该行被标记为deletion。您可以选择交换订单来决定是将值保留为dbsnp还是deletion

该代码处理所有列,而不考虑数据类型。

上述数据的输出为:

Number     Location    Pay      type
0    Mukul  Saharsanpur  25000     dbsnp
1    Rohan        MERrs  30000     dbsnp
2   Mayank      rsAdela  35000  deletion
3  Shubham    aaaadelaa  40000  deletion
4   Aakash          aaa  45000          

您可以使用str.contains,正如@Joe Ferndz所说:

# create filter based on your criteria
msk1 = df['Location'].str.contains('rs')
msk2 = df['Location'].str.contains('del')
# only make changes to those that fit the criteria
df.loc[msk1, 'Type'] = 'dbsnp'
df.loc[msk2, 'Type'] = 'deletion'
# if you wish to fill NaN with empty string
df['Type'] = df['Type'].fillna('')

此示例可以帮助您:

import pandas as pd
import random
inp = [{'c1':10, 'c2':100}, {'c1':11,'c2':110}, {'c1':12,'c2':120}]
df = pd.DataFrame(inp)
df['newColumn'] = ""
yourCondition = True
for i in range(len(df)):
# put your condition here
#
# if df['Name'].values[i].find("rs") != -1:
#    df['newColumn'].values[i] = "Found!"
# else:
#    df['newColumn'].values[i] = "Not Found!"
if (yourCondition):
# now you can update what you want
df['newColumn'].values[i] = random.randint(0,9)
print(df)

输出

c1   c2 newColumn
0  10  100         5
1  11  110         7
2  12  120         2

您可以添加这样的新列:df['newColumn'] = ""
并迭代和数据帧:for i in range(len(df)):然后你可以访问这样的元素:df['newColumn'].values[i]

最新更新