如何使用python编辑具有链接数据的列?

问题描述 投票:0回答:1

我有一个包含多列、行和部分的文本数据文件。在这里,我想删除第 5 或 6 或 7 列大于 50 的行。

挑战是:

  1. 在“Atoms”部分中,第一列称为“Atom_id”。此“Atom_id”用在“Bond”部分的第三或第四列中。因此,删除的“atom_id”也应该在“Bond”部分删除,如果它在第 3 或第 4 列可用的话。
  2. 将“Atom_id”和“Bond_id”更新为连续的。但是,问题出在“Bond”部分的第三或第四列,应该相应更新。

如有任何意见或建议,我们将不胜感激, 预先感谢。

这是我的最小化数据文件。

#Atoms
#Atom_id    molecules_id    atom_type   charge  x_coor  y_coor  z_coor
       1       1     2    -0.834           60.243           56.013           55.451  
       2       1     1     0.417           1.061            6.406            5.263  
       3       1     1     0.417           3.513            2.071             5.526  
       4       2     2    -0.834             4.14           6.861            5.328  
       5       2     1     0.417            4.322            6.96            5.317  
       6       2     1     0.417            3.303           1.922            4.912  
       7       3     2    -0.834           12.756           53.344            3.856  
       8       3     1     0.417           12.527           53.366            4.833  
       9       3     1     0.417           12.039           53.747            3.454  
      10       4     2    -0.834           1.402            7.122           13.392  
      
 #Bonds
 #Bond_id   bond_type   atom_id atom_id
       1       2       1       2    
       2       2       1       3    
       3       2       4       5  
       4       2       4       6   
       5       2       7       8   
       6       2       7       9   
       7       2      10      11   
       8       2      10      12   
       9       2      13      14   
      10       2      13      15    
      11       2      16      17 

看来最好清理数据并将这些部分分成两个不同的文件。为单独的部分编写脚本是否更容易?

python pandas data-science
1个回答
0
投票

如果我明白你的要求:我首先将文本文件分成两部分,然后使用 pd.read_csv 形成 2 个数据框(命名为原子和键),其形式如下所示:

   Atom_id  molecules_id  atom_type  charge  x_coor  y_coor  z_coor
0        1             1          2  -0.834  60.243  56.013  55.451
1        2             1          1   0.417   1.061   6.406   5.263
...
    Bond_id  bond_type  atom_id  atom_id.1
0         1          2        1          2
1         2          2        1          3
...

然后从原子 DF 中选择最后 3 行中任意一行 > 50 的所有行

atoms2 = atoms[(atoms['x_coor'].gt(50)) | (atoms['y_coor'].gt(50))| (atoms['z_coor'].gt(50))]

然后将满足此条件的 Atom_id 值保存在 List 中

atomID = atoms2['Atom_id'].to_list()

然后使用此列表从原始 DF 中删除行

atom3 = atoms[~(atoms['Atom_id'].isin(atomID))]

bonds2 = bonds[~((bonds['atom_id'].isin(atomID)) | (bonds['atom_id.1'].isin(atomID)))]

给出两个 DF

   Atom_id  molecules_id  atom_type  charge  x_coor  y_coor  z_coor
1        2             1          1   0.417   1.061   6.406   5.263
2        3             1          1   0.417   3.513   2.071   5.526
3        4             2          2  -0.834   4.140   6.861   5.328
4        5             2          1   0.417   4.322   6.960   5.317
5        6             2          1   0.417   3.303   1.922   4.912
9       10             4          2  -0.834   1.402   7.122  13.392

    Bond_id  bond_type  atom_id  atom_id.1
2         3          2        4          5
3         4          2        4          6
6         7          2       10         11
7         8          2       10         12
8         9          2       13         14
9        10          2       13         15
10       11          2       16         17

如果需要,您可以将这些 DF 写入文本文件或任何需要的文件。

© www.soinside.com 2019 - 2024. All rights reserved.