首页 > 解决方案 > 将列表转换为正确的数据框

问题描述

我正在尝试计算具有多个原子的系统的势能。到目前为止,我已经完成了在系统中组合 2 个随机原子的部分,并计算了该对之间的距离 r。

我得到了每对的 r,然后我将其放入函数 ljp,该函数根据距离 r 计算该对的 LJpotential。

现在我需要总结所有计算出的 ljp 值,我尝试通过将 ljp(r) 在列表中然后放入数据框来做到这一点。

但输出是这样的:

        0
0 -0.004894
          0
0 -0.005285
          0
0 -0.397155
          0
0 -0.238935
          0
0 -0.002175
          0
0 -0.021515
          0
0 -0.028713 ....

当所需的输出是:

     0
0 -0.004894
1 -0.005285
2 -0.397155 
.....

你能帮帮我吗?

import numpy as np

filename = 'lj-0200.xyz'
xyz_file = np.genfromtxt(fname=filename,skip_header=2,dtype='unicode')
xyz = open(filename)

atom_quantity = int(xyz.readline())
atom_coordinates = (xyz_file[:,1:])
atom_coordinates = atom_coordinates.astype(float)

print(type(atom_coordinates))
#print("The xyz coordinate of the each atom:",atom_coordinates)
import itertools as it
import math 
import numpy as np
import pandas as pd

combinations = it.combinations(atom_coordinates,2)
for combination in combinations:
    combination = np.array(combination)
    #print(combination)
    
    #array_xyz_sq = abs((combination[:1])**2 - (combination[1:2])**2)
    array_xyz_sq = abs((combination[:1])**2 - (combination[1:2])**2)
    #print(array_xyz_sq)
    
    # r^2 = ((x_1)^2 - (x_2)^2)+((y_1)^2 - (y_2)^2)+((z_1)^2 - (z_2)^2)
    array_xyz_sum =  np.sum(array_xyz_sq, axis = 1, keepdims = True)
    #print(array_xyz_sum)
    #print(type(array_xyz_sum))
    
    # Compute r, the distance 
    r = math.sqrt(array_xyz_sum)
    #print(r)
    #print(type(r))
    
    def ljp(n):
        # There are r=0 data, which will cause 1/0 error, so skip LJP calcualtion when r=0
        if n == 0:
            return 0
        else:
            # Formula from #1 assignment 
            return 4*1*((1/n)**12 - (1/n)**6)
   
    list_ljp = [ljp(r)]

    df = pd.DataFrame(list_ljp)
    print(df)

标签: listdataframecombinations

解决方案


推荐阅读