我有一个数据框,如下所示,
Head1 Header2
ABC SAP (+115590), GRN (+426250)
EFG HES3 (-6350), CMT (-1902)
HIJ CORT (-19440), API (+177)
KLM AAD (-25488), DH(-1341) ,DSQ(+120001)
SOS MFA (-11174), 13A2 (+19763)
我需要用逗号分隔第二列,并在同一数据帧中创建新列。除此之外,我还需要取出括号内的所有值,并使用该数字信息创建另一列以进一步筛选。
到目前为止,我可以用一段不那么优雅的代码来完成它,它很长,如下所示,
Trans = 'file.txt'
Trans = pd.read_csv(Trans, sep="\t", header=0)
Trans.columns=["RNA","PCs"]
# Here I changed the dtype to string to do split
Trans.PCs=Trans.PCs.astype(str)
#I took out those first part of second column into new column PC1
Trans["PC1"]=Trans.PCs.str.extract('(\w*)', expand=True)
#Here I splited the neuwmric informationf rom first part
Trans[['Strand1','Dis1']] = Trans.PCs.str.extract('([+-])(\d*)', expand=True)
Trans.head()
Head Header2 Head1 Strand1 Dis1
ABC SAP (+11559), GRN (+42625) SAP + 115590
EFG HES3 (-6350), CMT (-1902) HES3 - 6350
HIJ CORT (-19440), API (+177) CORT - 19440
KLM AAD (-25488), DH(-1341) AAD - 25488
SOS MFA (-11174), 13A2 (+19763) MFA - 11174
我需要再次拆分上面的数据帧,所以我在第2列的第二部分使用了下面的代码
# this for second part of 2nd column
Trans["PC2"]=Trans.PCs.str.split(',').str.get(1)
# did for neumric information
Trans[['Strand2','Dis2']] = Trans.PC2.str.extract('([+-])(\d*)', expand=True)
Trans['PC2']=Trans.PC2.str.replace(r"\(.*\)","")
# At this point the daframe looks like this,
Head Header2 Head1 Strand1 Dis1 Head2 Strand2 Dis2
ABC SAP (+11559), GRN (+42625) SAP + 115590 GRN + 426250
EFG HES3 (-6350), CMT (-1902) HES3 - 6350 CMT - 1902
HIJ CORT (-19440), API (+177) CORT - 19440 API + 177
KLM AAD (-25488), DH(-1341) AAD - 25488 DH - 1341
SOS MFA (-11174), 13A2 (+19763),DSQ(+120001) MFA - 11174 13A2 + 19763
Trans=Trans.fillna(0)
Trans.Dis1=Trans.Dis1.astype(int)
Trans.Dis2=Trans.Dis2.astype(int)
# Here I am filtering the rows based on Dis1 and Dis2 columns from daframe
> Trans_Pc1=Trans.loc[:,"lncRNA":"Dis1"].query('Dis1 >= 100000')
> Trans_Pc2=Trans.loc[:,"PC2":"Dis2"].query('Dis2 >= 100000')
> TransPC1=Trans_Pc1.PC1
> TransPC2=Trans_Pc2.PC2
> TransPCs=pd.concat([TransPC1,TransPC2])
看起来像这样,
Header
SAP
GRN
DSQ
尽管脚本很长,但当第二列的行中有两个以上逗号分隔的值时,我就有问题了,比如在这一行中,
KLM AAD (-25488), DH(-1341) ,DSQ(+120001)
它有三个逗号分隔的值,我知道我必须再次重复拆分,但我的数据帧非常大,有许多行的逗号分隔值不相等。例如,有些行的第2列有两个逗号分隔的值,有些行有5个逗号分隔的值,依此类推。
任何更好的方法来过滤我的框架将是伟大的。
最后,我的目标是一个数据帧,如下所示,
header
SAP
GRN
DSQ
任何帮助或建议都非常好
最佳答案
尝试:
df = pd.DataFrame(
[
['ABC', 'SAP (+115590), GRN (+426250)'],
['EFG', 'HES3 (-6350), CMT (-1902)'],
['HIJ', 'CORT (-19440), API (+177)'],
['KLM', 'AAD (-25488), DH(-1341) ,DSQ(+120001)'],
['SOS', 'MFA (-11174), 13A2 (+19763)'],
], columns=['Head1', 'Header2'])
df1 = df.Header2.str.split(',', expand=True)
regex = r'(?P<Head>\w+).*\((?P<Strand>[+-])(?P<Dis>.*)\)'
extract = lambda df: df.iloc[0].str.extract(regex, expand=True)
extracted = df1.groupby(level=0).apply(extract)
df2 = extracted.stack().unstack([2, 1])
colseries = df2.columns.to_series()
df2.columns = colseries.str.get(0).astype(str) + colseries.str.get(1).astype(str)
pd.concat([df, df2], axis=1)
关于python - 将列的值拆分为数据框内的新列以进行过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38242910/