如何搜索和获取 pandas 中每个唯一项的特殊字符数
How to search and get count of special characters for every unique item in pandas
通过使用 pandas 数据框,我试图获取列中特殊字符的数量,但未获得所需的输出。
我的 .tsv 文件是:input file
NAME ID
S. gambu NNNTYR
S. gambu RTD:78689
S. gambu GTT:67878
S. gambu RTD?78689
G. homos SFDG\SS234R
G. homos SFHG#SR234R
G. homos JHFG:EE345T
G. homos TYU=TT678R
T. mosus RTU,YY67ET
T. mosus TTR%YY67ET
G. ytrty NaN
我正在尝试计算每个特殊字符 (:\,#%=?) 形式 'ID' w.r.t 'NAME' (对于每个名称)(名称 =4 和special characters = 7) ,我没有得到 ID 中存在的每个名称和特殊字符的所需输出,这些字符是 "(:\,#%=?)"
我需要计算每个名字的每个特殊字符。
我试过了,但没有得到所需的输出。
期望的输出是:
NAME : \ # ? % = ,
S. gambu RTD:78689 0 0 RTD?78689 0 0 0
GTT:67878
count(2) count(1)
G. homos 0 SFDG\SS234R SFHG#SR234R 0 0 TYU=TT678R 0
count(1) count(1) count(1)
T. mosus 0 0 0 0 TTR%YY67ET 0 RTU,YY67ET
count(1) count(1)
G. ytrty NaN NaN NaN NaN NaN NaN NaN
Total 2 1 1 1 1 1 1
desired output
~~~python
我试过的代码:
pattern1 = [':','#',',','%',]
count= 0
count1 = 0
with open('name.txt') as f:
lines = f.read().splitlines()
for pat in pattern1:
pattern1 = re.compile(pat)
for line in lines:
for i in range(len(df3)):
if ((df3.loc[i,'NAME'] == line)):
if (pattern1.search(df3.loc[i,'ID'])):
count = count+1
out =str(df3.loc[i,'NAME'])+"\t"+str(df3.loc[i,'ID'])+"\n"
print(out)
~~~
为了获得所需的输出,我附加了 snap,因为它包含更多字段。
IIUC,
我们可以使用.str.count
请注意,我在 T. mosus
中添加了 3 个特殊字符。
data = d="""NAME ID
S. gambu NNNTYR
S. gambu RTD:78689
S. gambu RTD:78689
S. gambu RTD:78689
G. homos SFDG/SS234R
G. homos SFHG#SR234R
G. homos JHFG:EE345T
G. homos TYU=TT678R
T. mosus RTU@:/YY67ET
G. ytrty NaN"""
df = pd.read_csv(StringIO(data),sep='\s+')
pat = '[(:/,#%\=@)]'
df['count'] = df['ID'].str.count(pat)
print(df)
NAME ID count
S. gambu NNNTYR 0.0
S. gambu RTD:78689 1.0
S. gambu RTD:78689 1.0
S. gambu RTD:78689 1.0
G. homos SFDG/SS234R 1.0
G. homos SFHG#SR234R 1.0
G. homos JHFG:EE345T 1.0
G. homos TYU=TT678R 1.0
T. mosus RTU@:/YY67ET 3.0
G. ytrty NaN NaN
IIUC 这应该可以解决问题(df
是您的输入数据框)
import re
special_chars=r"(:/,#%=@)"
#to take care of nan-s
df=df.fillna("")
for el in special_chars:
temp_df=df["ID"].str.count(re.escape(el))
df[el]=np.where(temp_df.gt(0), df["ID"] + " (" + temp_df.astype(int).astype(str) + ")", None)
df2=df.groupby("NAME")[list(special_chars)].agg(lambda x: list(x[~x.isna()]))
我不完全确定你想要的值输出格式是什么 - 所以我把它放在一个列表中...
通过使用 pandas 数据框,我试图获取列中特殊字符的数量,但未获得所需的输出。
我的 .tsv 文件是:input file
NAME ID
S. gambu NNNTYR
S. gambu RTD:78689
S. gambu GTT:67878
S. gambu RTD?78689
G. homos SFDG\SS234R
G. homos SFHG#SR234R
G. homos JHFG:EE345T
G. homos TYU=TT678R
T. mosus RTU,YY67ET
T. mosus TTR%YY67ET
G. ytrty NaN
我正在尝试计算每个特殊字符 (:\,#%=?) 形式 'ID' w.r.t 'NAME' (对于每个名称)(名称 =4 和special characters = 7) ,我没有得到 ID 中存在的每个名称和特殊字符的所需输出,这些字符是 "(:\,#%=?)"
我需要计算每个名字的每个特殊字符。 我试过了,但没有得到所需的输出。
期望的输出是:
NAME : \ # ? % = ,
S. gambu RTD:78689 0 0 RTD?78689 0 0 0
GTT:67878
count(2) count(1)
G. homos 0 SFDG\SS234R SFHG#SR234R 0 0 TYU=TT678R 0
count(1) count(1) count(1)
T. mosus 0 0 0 0 TTR%YY67ET 0 RTU,YY67ET
count(1) count(1)
G. ytrty NaN NaN NaN NaN NaN NaN NaN
Total 2 1 1 1 1 1 1
desired output
~~~python
我试过的代码:
pattern1 = [':','#',',','%',]
count= 0
count1 = 0
with open('name.txt') as f:
lines = f.read().splitlines()
for pat in pattern1:
pattern1 = re.compile(pat)
for line in lines:
for i in range(len(df3)):
if ((df3.loc[i,'NAME'] == line)):
if (pattern1.search(df3.loc[i,'ID'])):
count = count+1
out =str(df3.loc[i,'NAME'])+"\t"+str(df3.loc[i,'ID'])+"\n"
print(out)
~~~
为了获得所需的输出,我附加了 snap,因为它包含更多字段。
IIUC,
我们可以使用.str.count
请注意,我在 T. mosus
中添加了 3 个特殊字符。
data = d="""NAME ID
S. gambu NNNTYR
S. gambu RTD:78689
S. gambu RTD:78689
S. gambu RTD:78689
G. homos SFDG/SS234R
G. homos SFHG#SR234R
G. homos JHFG:EE345T
G. homos TYU=TT678R
T. mosus RTU@:/YY67ET
G. ytrty NaN"""
df = pd.read_csv(StringIO(data),sep='\s+')
pat = '[(:/,#%\=@)]'
df['count'] = df['ID'].str.count(pat)
print(df)
NAME ID count
S. gambu NNNTYR 0.0
S. gambu RTD:78689 1.0
S. gambu RTD:78689 1.0
S. gambu RTD:78689 1.0
G. homos SFDG/SS234R 1.0
G. homos SFHG#SR234R 1.0
G. homos JHFG:EE345T 1.0
G. homos TYU=TT678R 1.0
T. mosus RTU@:/YY67ET 3.0
G. ytrty NaN NaN
IIUC 这应该可以解决问题(df
是您的输入数据框)
import re
special_chars=r"(:/,#%=@)"
#to take care of nan-s
df=df.fillna("")
for el in special_chars:
temp_df=df["ID"].str.count(re.escape(el))
df[el]=np.where(temp_df.gt(0), df["ID"] + " (" + temp_df.astype(int).astype(str) + ")", None)
df2=df.groupby("NAME")[list(special_chars)].agg(lambda x: list(x[~x.isna()]))
我不完全确定你想要的值输出格式是什么 - 所以我把它放在一个列表中...