python-3.x - 如何迭代 dfs 并使用组合名称附加数据

标签 python-3.x pandas slice

我有这个问题要解决,这是上一个问题的延续How to iterate over pandas df with a def function variable function给定的答案完美地工作,但现在我必须将所有数据附加到 2 列数据框中(Adduct_name 和质量)。

这是来自上一个问题:

我的目标:我必须计算给定“化合物”的“加合物”,两者都代表数字,但对于每个“化合物”有 46 种不同的“加合物”。

每个加合物的计算如下:

加合物 1 = [精确质量*M/电荷 + 加合物质量]

其中,精确质量 = 数字,M 和电荷 = 根据每种加合物类型的数字(1、2、3 等),Adduct_mass = 根据每种加合物的数字(正或负)。

我的数据:2个数据框。其中包含加合物名称、M、电荷、加合物质量。另一个对应于我想要迭代的化合物的Compound_name和Exact_mass(我只放了一个小数据集)

加合物:df_al

import pandas as pd 
data = [["M+3H", 3, 1, 1.007276], ["M+3Na", 3, 1, 22.989], ["M+H", 1, 1, 
1.007276], ["2M+H", 1, 2, 1.007276], ["M-3H", 3, 1, -1.007276]]
df_al = pd.DataFrame(data, columns=["Ion_name", "Charge", "M", "Adduct_mass"])

化合物:df

import pandas as pd 
data1 = [[1, "C3H64O7", 596.465179], [2, "C30H42O7", 514.293038], [4, 
"C44H56O8", 712.397498], [4, "C24H32O6S", 448.191949], [5, "C20H28O3", 
316.203834]]
df = pd.DataFrame(data1, columns=["CdId", "Formula", "exact_mass"])

这个问题的解决办法是:

df_name = df_al["Ion_name"]
df_mass = df_al["Adduct_mass"]
df_div = df_al["Charge"]
df_M = df_al["M"]
#Defining general function 
def Adduct(x,i):
    return x*df_M[i]/df_div[i] + df_mass[i]

#Applying general function in a range from 0 to 5. 
for i in range(5):
    df[df_name.loc[i]] = df['exact_mass'].map(lambda x: Adduct(x,i))

输出

    Name exact_mass  M+3H       M+3Na        M+H        2M+H        M-3H
0   a   596.465179  199.829002  221.810726  597.472455  1193.937634 197.814450
1   b   514.293038  172.438289  194.420013  515.300314  1029.593352 170.423737
2   c   712.397498  238.473109  260.454833  713.404774  1425.802272 236.458557
3   d   448.191949  150.404592  172.386316  449.199225  897.391174  148.390040
4   e   316.203834  106.408554  128.390278  317.211110  633.414944  104.39400

现在这是正确的计算,但我现在需要一个文件,其中: -仅存在 2 列(名称和质量) -所有不同的加合物都依次附加

期望的输出

 Name     Mass 
 a_M+3H  199.82902
 a_M+3Na 221.810726
 a_M+H   597.472455
 a_2M+H  1193.937634
 a_M-3H  197.814450 
 b_M+3H  514.293038
 .
 . 
 .
 c_M+3H

等等。

我还需要将各个化合物的名称与 ionic 形式(M+3H、M+H 等)结合起来。

目前我还没有代码。

从一开始我就很感激任何建议和更好的方法。


这部分是对上述问题的更新:

可以像这样获取和输出:

  Name     Mass       RT
 a_M+3H  199.82902     1
 a_M+3Na 221.810726    1
 a_M+H   597.472455    1
 a_2M+H  1193.937634   1
 a_M-3H  197.814450    1
 b_M+3H  514.293038    3
 .           
 . 
 .
 c_M+3H                2

所有形式的化合物的 RT 值相同,在此示例中,a = 1、b = 3、c = 2 等的 RT 值。

是否可以从数据集 df(我在下面更新)中合并(保留此列)?正如您所看到的, df 有更多的列,例如“公式”和“RT”,这些列在计算后消失。

import pandas as pd 
data1 = [[a, "C3H64O7", 596.465179, 1], [b, "C30H42O7", 514.293038, 3], [c, 
"C44H56O8", 712.397498, 2], [d, "C24H32O6S", 448.191949, 4], [e, "C20H28O3", 
316.203834, 1.5]]
df = pd.DataFrame(data1, columns=["Name", "Formula", "exact_mass", "RT"])  

第三部分! (抱歉,谢谢)

这是我使用下面的代码在小数据集(df)上进行的试验,与上面的 df_al 相同。

df= enter image description here

代码

#Defining variables for calculation

df_name = df_al["Ion_name"]
df_mass = df_al["Adduct_mass"]
df_div = df_al["Charge"]
df_M = df_al["M"]
df_ID= df["Name"]

#Defining the RT dictionary

RT = dict(zip(df["Name"], df["RT"]))

#Removing RT column

df=df.drop(columns=["RT"])

#Defining general function 

def Adduct(x,i):
    return x*df_M[i]/df_div[i] + df_mass[i]

#Applying general function in a range from 0 to 46. 

for i in range(47):
    df[df_name.loc[i]] = df['exact_mass'].map(lambda x: Adduct(x,i)) 

df 

输出

enter image description here

#Melting

df = pd.melt(df, id_vars=['Name'], var_name = "Adduct", value_name= "Exact_mass", value_vars=[x for x in df.columns if 'Name' not in x and 'exact' not in x])

df['name'] = df.apply(lambda x:x[0] + "_" + x[1], axis=1)

df['RT'] = df.Name.apply(lambda x: RT[x[0]] if x[0] in RT else np.nan)

del df['Name']

del df['Adduct']

df['RT'] = df.name.apply(lambda x: RT[x[0]] if x[0] in RT else np.nan)

df

输出

enter image description here

为什么是 NaN?

最佳答案

这是我将如何解决这个问题,pandas.melt 来救援:

import pandas as pd
import numpy as np

from io import StringIO

s = StringIO('''
    Name exact_mass  M+3H       M+3Na        M+H        2M+H        M-3H
0   a   596.465179  199.829002  221.810726  597.472455  1193.937634 197.814450
1   b   514.293038  172.438289  194.420013  515.300314  1029.593352 170.423737
2   c   712.397498  238.473109  260.454833  713.404774  1425.802272 236.458557
3   d   448.191949  150.404592  172.386316  449.199225  897.391174  148.390040
4   e   316.203834  106.408554  128.390278  317.211110  633.414944  104.39400
''')

df = pd.read_csv(s, sep="\s+")

df = pd.melt(df, id_vars=['Name'], value_vars=[x for x in df.columns if 'Name' not in x and 'exact' not in x])



df['name'] = df.apply(lambda x:x[0] + "_" + x[1], axis=1)

del df['Name']
del df['variable']


RT = {'a':1, 'b':2, 'c':3, 'd':5, 'e':1.5}

df['RT'] = df.name.apply(lambda x: RT[x[0]] if x[0] in RT else np.nan)
df

这是输出:

enter image description here

关于python-3.x - 如何迭代 dfs 并使用组合名称附加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233679/

相关文章:

python-3.x - cv2 imshow 与 matplotlib imshow 有何巨大差异?

python-3.x - 非拉丁字符的 Python 正则表达式不起作用

python - 使用逗号时,除了处理程序中的语法无效

python - pandas-resetting cumsum 在一个特定的数字

python - 加速矩阵中某些列的求和

python - 字符串格式化和解析

python - 如何用python计算上个月的频率?

python - 读取压缩在一个文件中的多个 csv 文件

python - 当切片本身是 tensorflow 中的张量时如何进行切片分配

python - Theano 中的切片和索引