2016-07-07 102 views
3

我有一个数据帧,它看起来如下,一个数据帧中的分裂列到新列的值过滤

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 

即使脚本是漫长的工作,但我有问题,当第二列有喜欢这里的分隔值超过2个逗号行行,

KLM AAD (-25488), DH(-1341) ,DSQ(+120001) 

它有三个逗号分隔值,我知道我必须再次重复分裂,但我的数据帧是非常大的,有马ny行不等逗号分隔值。例如,某些行的第2列有2个逗号分隔值,有些行的逗号分隔值为5,依此类推。

任何更好的方式来筛选我的框架将是伟大的。 最终,我的目标一个数据帧如下,

header 
SAP 
GRN 
DSQ 

任何帮助或建议将是真正伟大

回答

1

尝试:

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) 

enter image description here

+0

谢谢你的简单方法,但正如你可以在后期看到的,我需要进一步基于Dis *列进行过滤,并返回那些Dis *> = 100000的行,所以当有很多Dis *列时我的意思是,以下是当我只有两个Dis列时,我尝试了Trans_Pc1 = Trans.loc [:,“Head1”:“Dis1”]。query('Dis1> = 100000') > Trans_Pc2 = Trans.loc [:,“Head2”:“Dis2”]。query('Dis2> = 100000') – user1017373

相关问题