试图在DataFrame分配数据框中的切片副本上设置一个值

时间:2020-07-03 10:07:11

标签: python-3.x pandas

有什么方法可以重构代码以避免收到此警告?

试图在DataFrame的切片副本上设置一个值。尝试 使用.loc [row_indexer,col_indexer] =值

# get the total number of users who paid off
paidOffTotaDataFrame = newDF.loc[(newDF.Loan_Status=='PAIDOFF')]

#create column paid off in X days
difference = newDF.Paid_Off_Time - newDF.Effective_Date
paidOffTotaDataFrame['Paid_Off_In_Days'] = difference

我也尝试过

paidOffTotaDataFrame.loc[:,'Paid_Off_In_Days'] = difference

print(paidOffTotaDataFrame.apply(lambda row: "paid off column " + str(row['Paid_Off_In_Days']), axis=1))

1 个答案:

答案 0 :(得分:1)

  1. 复制切片,因此它是新的df,而不是对旧df的切片的引用:paidOffTotaDataFrame = newDF.loc[(newDF.Loan_Status=='PAIDOFF')].copy()

  2. 仅使用新的df:paidOffTotaDataFrame['Paid_Off_In_Days'] = paidOffTotaDataFrame.Paid_Off_Time - paidOffTotaDataFrame.Effective_Date