python - 给定一列具有特定值的情况下编辑数据框的行

标签 python pandas dataframe

我有一个大型数据框df_Trial,其中一行名为reaction。其中有五个值,可能是:152Gd-p154Gd-p155Gd-p156Gd-p 15Gd-p158Gd-p160Gd-p。以下列包含一些与此步骤无关的信息,但是我希望根据“reaction”中存在的字符串将以下列乘以一个常数。我尝试将此应用为:

for index, row in df_trial.iterrows():
    if row['reaction'] == '152Gd-p':
        row[2:]*=0.002
    if row['reaction'] == '154Gd-p':
        row[2:]*=0.0218
    if row['reaction'] == '155Gd-p':
        row[2:]*=0.148
    if row['reaction'] == '156Gd-p':
        row[2:]*=0.2047
    if row['reaction'] == '157Gd-p':
        row[2:]*=0.1565
    if row['reaction'] == '158Gd-p':
        row[2:]*=0.2484
    if row['reaction'] == '160Gd-p':
        row[2:]*=0.2186

但这并没有将行中的值相乘。

这是我的数据框外观的示例:

        reaction    product 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 ... 35.5    36.0    36.5    37.0    37.5    38.0    38.5    39.0    39.5    40.0
81  155Gd-p 062150.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 1.101530e-02    1.253720e-02    1.404620e-02    1.562480e-02    1.713510e-02    1.855860e-02    1.989160e-02    2.113160e-02    2.228710e-02    2.333700e-02
82  155Gd-p 065156.L00  0.0 0.0 0.0 0.0 2.842720e-10    4.331690e-09    3.176340e-08    1.873100e-03    ... 1.836500e-01    1.803630e-01    1.728360e-01    1.606180e-01    1.685970e-01    1.679980e-01    1.639340e-01    1.538330e-01    1.639280e-01    1.656980e-01
83  155Gd-p 063149.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 6.990920e+00    7.877440e+00    8.781220e+00    9.594980e+00    1.034780e+01    1.097080e+01    1.156940e+01    1.196730e+01    1.230900e+01    1.241800e+01
84  155Gd-p 061146.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    9.529110e-04
85  155Gd-p 061147.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 1.000000e-07    1.000000e-07    1.000000e-07    1.000000e-07    1.000000e-07    1.063060e-03    1.130870e-03    1.172590e-03    1.180610e-03    1.165960e-03
86  155Gd-p 062151.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    1.000000e-07    1.000000e-07    ... 1.041300e-03    1.076720e-03    1.090690e-03    1.109420e-03    1.137780e-03    1.135450e-03    1.128680e-03    1.149190e-03    1.143860e-03    1.150390e-03
87  155Gd-p 063154.L00  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 3.173660e+00    3.444480e+00    3.724070e+00    4.007600e+00    4.323930e+00    4.673050e+00    4.971810e+00    5.346970e+00    5.661720e+00    6.060110e+00
88  155Gd-p 064150.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 2.579220e-03    4.992070e-03    9.679620e-03    2.035170e-02    3.447910e-02    5.437450e-02    9.089650e-02    1.471190e-01    2.126190e-01    2.896810e-01
89  155Gd-p 064154.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 2.337670e+02    2.386140e+02    2.390300e+02    2.431350e+02    2.413420e+02    2.446070e+02    2.421500e+02    2.447660e+02    2.425080e+02    2.446910e+02
90  155Gd-p 062148.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    1.172560e-03    1.722920e-03
91  155Gd-p 061148.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 1.000000e-07    1.000000e-07    1.000000e-07    1.000000e-07    1.000000e-07    6.300980e-05    6.278910e-05    6.094680e-05    5.998620e-05    5.900480e-05
92  155Gd-p 063153.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 3.136120e+00    3.390600e+00    3.631630e+00    3.958020e+00    4.197170e+00    4.564750e+00    4.762130e+00    4.948580e+00    5.314960e+00    5.549590e+00
93  155Gd-p 063152.tot  0.0 0.0 0.0 0.0 1.000000e-07    1.000000e-07    1.000000e-07    1.000000e-07    ... 2.371420e+00    2.502180e+00    2.629100e+00    2.699470e+00    2.818750e+00    2.972010e+00    3.188610e+00    3.416830e+00    3.648320e+00    3.884690e+00
94  155Gd-p 065151.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 6.969380e+01    1.072870e+02    1.483310e+02    1.996210e+02    2.548350e+02    3.046590e+02    3.501350e+02    3.969820e+02    4.371780e+02    4.748900e+02
95  155Gd-p 063150.L01  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 2.297790e+00    2.303140e+00    2.279280e+00    2.212180e+00    2.171240e+00    2.134990e+00    2.086730e+00    2.017590e+00    1.979700e+00    1.957500e+00
96  155Gd-p 065152.tot  0.0 0.0 0.0 0.0 0.000000e+00    0.000000e+00    0.000000e+00    0.000000e+00    ... 7.834620e+02    7.493000e+02    7.095910e+02    6.583400e+02    6.050390e+02    5.525560e+02    5.055030e+02    4.521510e+02    4.095180e+02    3.664050e+02

显然,在完整的数据框中,它包含包含所有反应的行。

最佳答案

您可以定义一个函数来乘以每行,然后将其应用到每行 apply .

此外,为了改善条件,您可以设置 elif 而不是 if。这可以防止每次都测试所有条件。

在答案中,我使用字典来保存系数。然后,我通过 reaction 键调用它。

代码如下:

# Coefficients
multi_coef = {
    '152Gd-p': 0.002,
    '154Gd-p': 0.0218,
    '155Gd-p': 0.148,
    '156Gd-p': 0.2047,
    '157Gd-p': 0.1565,
    '158Gd-p': 0.2484,
    '160Gd-p': 0.2186,
}
# Get columns name
columns = df.columns

# Function to apply to each row
def multiply_coef(row):
    # If the reaction name is in the dictionary
    if row.reaction in multi_coef.keys():
        # Multiply by the reaction coefficient
        row[columns[2:]] = row[columns[2:]] * multi_coef[row.reaction]
    return row

# Apply the function (axis = 1 means over rows)
new_df = df.apply(multiply_coef, axis=1)

print(new_df)
#    reaction     product  0.5  1.0  1.5  2.0  ...          37.5       38.0       38.5       39.0       39.5       40.0
# 0   155Gd-p  062150.tot  0.0  0.0  0.0  0.0  ...  2.535995e-03   0.002747   0.002944   0.003127   0.003298   0.003454
# 1   155Gd-p  065156.L00  0.0  0.0  0.0  0.0  ...  2.495236e-02   0.024864   0.024262   0.022767   0.024261   0.024523
# 2   155Gd-p  063149.tot  0.0  0.0  0.0  0.0  ...  1.531474e+00   1.623678   1.712271   1.771160   1.821732   1.837864
# 3   155Gd-p  061146.tot  0.0  0.0  0.0  0.0  ...  0.000000e+00   0.000000   0.000000   0.000000   0.000000   0.000141
# 4   155Gd-p  061147.tot  0.0  0.0  0.0  0.0  ...  1.480000e-08   0.000157   0.000167   0.000174   0.000175   0.000173
# 5   155Gd-p  062151.tot  0.0  0.0  0.0  0.0  ...  1.683914e-04   0.000168   0.000167   0.000170   0.000169   0.000170
# 6   155Gd-p  063154.L00  0.0  0.0  0.0  0.0  ...  6.399416e-01   0.691611   0.735828   0.791352   0.837935   0.896896
# 7   155Gd-p  064150.tot  0.0  0.0  0.0  0.0  ...  5.102907e-03   0.008047   0.013453   0.021774   0.031468   0.042873
# 8   155Gd-p  064154.tot  0.0  0.0  0.0  0.0  ...  3.571862e+01  36.201836  35.838200  36.225368  35.891184  36.214268
# 9   155Gd-p  062148.tot  0.0  0.0  0.0  0.0  ...  0.000000e+00   0.000000   0.000000   0.000000   0.000174   0.000255
# 10  155Gd-p  061148.tot  0.0  0.0  0.0  0.0  ...  1.480000e-08   0.000009   0.000009   0.000009   0.000009   0.000009
# 11  155Gd-p  063153.tot  0.0  0.0  0.0  0.0  ...  6.211812e-01   0.675583   0.704795   0.732390   0.786614   0.821339
# 12  155Gd-p  063152.tot  0.0  0.0  0.0  0.0  ...  4.171750e-01   0.439857   0.471914   0.505691   0.539951   0.574934
# 13  155Gd-p  065151.tot  0.0  0.0  0.0  0.0  ...  3.771558e+01  45.089532  51.819980  58.753336  64.702344  70.283720
# 14  155Gd-p  063150.L01  0.0  0.0  0.0  0.0  ...  3.213435e-01   0.315979   0.308836   0.298603   0.292996   0.289710
# 15  155Gd-p  065152.tot  0.0  0.0  0.0  0.0  ...  8.954577e+01  81.778288  74.814444  66.918348  60.608664  54.227940

关于python - 给定一列具有特定值的情况下编辑数据框的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57114422/

相关文章:

python - python中大型数据集的后向消除

python - 将 describe 函数应用于 pandas groupby 时如何忽略行的最大值和最小值

python - 无法将数据写入数据框的 csv 文件中的换行符

python - 如何让pyqt5开发的应用程序在ubuntu上支持中文输入

具有依赖关系的python argparse

python - 将映射元素按元素应用于系列上的每个元素(Pandas)

python - 只提取 2 个字符的单词 Pandas Series

python - 对每一列相关性独立排序并得到索引值

python - 如何合并多个数据框

r - 为什么 mapply 不能按预期使用转换?