在数据帧中将列的值拆分为新列以实现fi

2024-06-26 14:12:43 发布

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

我有一个数据帧,如下所示

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列有2个逗号分隔的值,有些行有5个逗号分隔的值,以此类推。你知道吗

任何更好的方法来过滤我的框架将是伟大的。 最后,我的目标是一个数据帧,如下所示

header
SAP
GRN
DSQ

任何帮助或建议都会非常好


Tags: 数据transgrndhsap逗号pcsstr
1条回答
网友
1楼 · 发布于 2024-06-26 14:12:43

尝试:

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

相关问题 更多 >