【问题标题】:Getting unwanted values in a new column with pandas append in a for loop在 for 循环中附加 pandas 在新列中获取不需要的值
【发布时间】:2019-06-13 12:09:00
【问题描述】:

我正在尝试制作一个脚本,该脚本循环遍历数据框中的行,并根据 C 列中的条件从 A 列或 B 列附加值创建一个新列。但是,在附加列中的行,因为我的新列包含多个值。

import pandas as pd
import numpy as np

#Loading in the csv file
filename = '35180_TRA_data.csv'
df1 = pd.read_csv(filename, sep=',', nrows=1300, skiprows=25, index_col=False, header=0)

#Calculating the B concentration using column A and a factor
B_calc = df1['A']*137.818

#The measured B concentration
B_measured = df1['B']

#Looping through the dataset, and append the B_calc values where the C column is 2, while appending the B_measured values where the C column is 1.
calculations = []

for row in df1['C']:
    if row == 2:
        calculations.append(B_calc)
    if row ==1:
        calculations.append(B_measured)

df1['B_new'] = calculations

我的新列(B_new)的值都是错误的。例如,在第一行中它应该只是 0.00,但它包含许多值。所以在附录中出了点问题。谁能发现这个问题?

【问题讨论】:

  • 看起来您的 calculations 是一个系列数组,因为每次您将一个系列附加到它。如果可能,您应该避免循环行。相反,使用布尔掩码或np.where
  • @QuangHoang:谢谢!我使用了 np.where 并且成功了。

标签: python pandas


【解决方案1】:

B_calc 和 B_measured 是数组。因此,您必须指定要分配的值,否则您分配整个数组。你可以这样做:

df1 = pd.DataFrame({"A":[1,3,5,7,9], "B" : [9,7,5,3,1], "C":[1,2,1,2,1]})
#Calculating the B concentration using column A and a factor
B_calc = df1['A']*137.818

#The measured B concentration
B_measured = df1['B']
#Looping through the dataset, and append the B_calc values where the C column is 2, while appending the B_measured values where the C column is 1.
calculations = []

for index, row in df1.iterrows():
    if row['C'] == 2:
        calculations.append(B_calc[index])
    if row['C'] ==1:
        calculations.append(B_measured[index])

df1['B_new'] = calculations

但是对行进行迭代是一种不好的做法,因为它需要很长时间。更好的方法是使用 pandas 掩码,它是这样工作的:

mask_1 = df1['C'] == 1
mask_2 = df1['C'] == 2

df1.loc[mask_1, 'C'] = df1[mask_1]['A']*137.818
df1.loc[mask_2, 'C'] = df1[mask_2]['B']

【讨论】:

  • 感谢您的建议!我按照上面评论中的建议使用 np.where 解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2021-09-14
  • 2021-07-10
  • 2020-03-26
  • 2021-02-15
  • 2017-03-06
  • 2021-10-15
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多