【问题标题】:Python: Converting a list of strings into pandas data-frame with two columns a and b, corresponding to odd and even strings respectivelyPython:将字符串列表转换为具有两列a和b的pandas数据框,分别对应奇数和偶数字符串
【发布时间】:2021-08-31 05:49:08
【问题描述】:

我有这样的输入如下。它是一个字符串列表,每个奇数字符串都是一个以 MR 开头的数字,每个偶数字符串都是一些混合文本。我需要将此字符串列表转换为严格包含两列的 pandas 数据框,但由于某些 MR 数字与不同的混合文本计数器部分配对多次出现,因此在重复 MR 的地方我都会得到额外的列,正如我在下面演示的那样:

list = ['MR4044696:',
 'The GAP Group, GAP – groups, algorithms and programming, version 4.10, Available from http://www.gap-system.org, 2018.',
 'MR2900886:',
 'The GAP Group, $GAP$ groups, algorithms, and programming, version 4.4.12 (2008), http://www.gap-system.org.',
 'MR3169623:',
 'Distler, A., Mitchell, J. D. (2011). Smallsemi - A Library of Small Semigroups. Oct A GAP 4 package [5], Version 0.6.4.',
 'MR3169623:',
 'The GAP Group, (2008). (http://www.gap-system.org). GAP–Groups, Algorithms, and Programming, Version 4.4.12.',
 'MR4180136:',
 'The GAP Group, 2019. GAP – Groups, Algorithms, and Programming, Version 4.10.1; https://www.gap-system.org.',
 'MR11:',
 'GAP group',
 'MR1111111:',
 'Distler, A., Mitchell, J. D. (2011). Smallsemi - A Library of Small Semigroups. Oct A GAP 4 package [5], Version 0.6.4.',
 'MR1111111:',
 'The GAP Group, (2008). (http://www.gap-system.org). GAP–Groups, Algorithms, and Programming, Version 4.4.12.',
 'MR5:',
 'V. A. Artamonov and A. A. Bovdi, Integral gro GAP up rings: Groups of invertible elements and classical $K$-theory, in Algebra, Topology, Geometry, Vol. 27 (Russian), Itogi Nauki i Tekhniki, 232. (Vsesoyuz. Inst. Nauchn. i Tekhn. Inform., Moscow, 1989), pp. 3–43. \nMR1039822',
 'MR5:',
 "V. Bovdi, A. Grishkov and A. Konovalov, Kimmerle @GAP a lapa conjecture for the Held and O'Nan sporadic simple groups, Sci. Math. Jpn. 69(3) (2009) 353–361. \nMR2510100",
 'MR5:',
 'V. Bovdi and A. Konovalov, Integral group ringaper GAP? eg of the McLaughlin simple group, Algebra Discrete Math. 2 (2007) 43–53. \nMR2364062',
 'MR7:',
 'V. A. Artamonov and A. A. Bovdi, Integral gro GAP up rings: Groups of invertible elements and classical $K$-theory, in Algebra, Topology, Geometry, Vol. 27 (Russian), Itogi Nauki i Tekhniki, 232. (Vsesoyuz. Inst. Nauchn. i Tekhn. Inform., Moscow, 1989), pp. 3–43. \nMR1039822',
 'MR7:',
 "V. Bovdi, A. Grishkov and A. Konovalov, Kimmerle @GAP a lapa conjecture for the Held and O'Nan sporadic simple groups, Sci. Math. Jpn. 69(3) (2009) 353–361. \nMR2510100",
 'MR7:',
 'V. Bovdi, E. Jespers and A. Konovalov, Tors gap ion gap GAP gappen units in integral group rings of Janko sgapimple groups, Math. Comp. 80 (2011) 593–615. \nMR2728996',
 'MR7:',
 'V. Bovdi and A. Konovalov, Integral group ringaper GAP? eg of the McLaughlin simple group, Algebra Discrete Math. 2 (2007) 43–53. \nMR2364062',
 'MR9:',
 'The GAP Group, $GAP$ groups, algorithms, and programming, version 4.4.12 (2008), http://www.gap-system.org.']

我需要转换为 pandas df,其中一列用于 MR 编号,另一列用于其余部分。 我试过 df = pd.DataFrame(list) ,但因为一些 MR 数字重复,我得到了额外的列。我知道这可能是一个愚蠢的问题,但我无法解决,谢谢。

我只需要两列,如果有重复的 MR 编号,我需要它在单独的行中,例如:

MR   Rest
111   'asd'
222   'fgh'
333   'fff'
333   'ghj'
444   'yyy'
555   'hjk'
555   'jkl'

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    尝试:

    df=pd.DataFrame(lst)  #here lst is your list...Don't assign anything to list function
    c=df.index%2==0  #checking if the index is even bcz the values are in consicutive order 
    out=pd.concat((df.loc[c,0].str.strip(':').reset_index(drop=True),df[~c].reset_index()),axis=1).drop('index',1)
    #seperating out rows and concat them so that they can become new columns
    out.columns=['MR','Rest']  #Finally renaming the columns
    

    df=pd.DataFrame(lst)  #here lst is your list...Don't assign anything to list function
    c=df.index%2==0  #checking if the index is even bcz the values are in consicutive order
    out=pd.DataFrame([df.loc[c,0].str.strip(':').tolist(),df.loc[~c,0].tolist()],index=['MR','Rest']).T
    

    如果你打印out,你会得到你想要的输出

    如果需要删除重复值:

    out=out.drop_duplicates(subset=['MR'])
    #removing duplicated entries on basis of MR column
    

    【讨论】:

      【解决方案2】:

      试试这个

      import pandas as pd
      
      index = [list[i] for i in range(0, len(list), 2)]
      data = [list[i + 1] for i in range(0, len(list), 2)]
      
      df = pd.DataFrame(data, index=index)\
          .reset_index()\
          .rename(columns={'index': 'MR', 0: 'Rest'})
      

      【讨论】:

      • 嗨罗德里戈不幸的是这不起作用,它还会破坏所有重复的 MR 号码。我试图修复它,但没有好处。
      • 对不起,我编辑了代码。现在也许它更有用
      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 2021-06-15
      • 2019-07-09
      • 2011-04-11
      • 1970-01-01
      • 2019-06-03
      • 2018-01-08
      • 1970-01-01
      相关资源
      最近更新 更多