I am trying to add values to a new dataframe (df2) column (Discount%), the values in this column must be based on "Grid" and Entity from df1), My structure is as following enter image description here

因此,如果对于同一实体,DF1中的列是91-120,那么它应该在折扣%下将DF2加20,如果DF1中的列是61-90,那么它必须将5加到DF2,依此类推.

数据是从一个大的CSV文件导入的,到目前为止我已经try 过了,但如果只填0

for j in range(0,len(df1)):
for i in range(0,len(df2)):
if grid['91-120'][j] in df2['Grid'][i]:
#df['Grid%'][i] = grid['91-120'][j]
df2.loc[i, 'Grid%'] = df1['91-120'][j]

谢谢

推荐答案

我目前正在处理一个与在数据帧上迭代有关的类似问题.如果可以避免,您really不希望这样做,特别是当数据帧包含像您的df1这样的重复值时. 我建议将引用数据帧DF1转换为具有索引方向的字典,然后将该字典中的值赋给DF2,如下所示.

DF1 = pd.DataFrame({'Entity': ['F1', 'F2', 'F3', 'F4'], '0-60': [0, 0, 0, 0], '61-90': [0, 5, 10, 5], '91-120':[20, 5, 20, 20], '121-180':[10, 5, 12, 15], '181-240':[20, 5, 22, 25]})
DF2 = pd.DataFrame({'Entity': ['F1', 'F2', 'F3', 'F4'], 'Grid': ['360+', '61-90', '0-60', '91-120']})

print('DF2 before:')
print(DF2)

DF1.drop_duplicates(inplace=True)
DF1.set_index('Entity', inplace=True)
d = DF1.to_dict('index')

def get_discount(entity, grid):
    if entity in d and grid in d[entity]:
        return d[entity][grid]
    else:
        return None

DF2['Discount %'] = DF2.apply(lambda x: get_discount(x['Entity'], x['Grid']), axis=1)

print('DF2 after:')
print(DF2)

I found this solution because, as I mentioned before, I'm currently working on a similar problem.
Knowing how detrimental iteration over a dataframe can be to the performance of a function, I realized it would be faster to assign a value from a dictionary. I looked up how to convert a dataframe to a dictionary on Stack Overflow and in the pandas documentation. Next, I looked up how to assign a value to a dataframe from a dictionary on Stack Overflow. I was trying the "dict" orientation at first. I could get it to assign all discount values for each "Entity" based on "Grid", but I couldn't select the one right discount value.
I couldn't find any other solutions online for assigning a value to a dataframe from a 2D dictionary, so I turned to ChatGPT. After ChatGPT did its thing, I was getting "None" in every field. Eventually, I got it to recommend changing the orientation of the dictionary from "dict" to "series". That also didn't work, but I figured I would try all the other orientations. Index worked.
The downside is that Discount % values are floats. The upside is it can handle cases where you don't have a value for Grid in the dictionary (e.g. where Grid is "360+").

Python-3.x相关问答推荐

Python将类实例变量转换为嵌套 struct

Python GUI:tkinter应用程序作为Windows的实时桌面

类变量的Python子类被视为类方法

重复数组直到一定长度 groupby pandas

如何将函数映射到所有命名元组的元素?

Pytest顺序测试A,然后测试B,然后再测试A

GEKKO 在没有不等式的模型中抛出不等式定义错误

Python3:是否可以将变量用作函数调用的一部分

集合操作:应该只适用于集合,但适用于 dict_keys?

Python从base64转换为二进制

pythondecorator中的变量范围

Python 3.9.8 使用 Black 并导入 `typed_ast.ast3` 失败

Pandas 的 EMA 与股票的 EMA 不匹配?

为什么中断比引发异常更快?

python判断一个方法是否被调用而不模拟它

带有自定义标头的 urllib.urlretrieve

有没有一种标准方法来确保 python 脚本将由 python2 而不是 python3 解释?

将 Python SIGINT 重置为默认信号处理程序

为什么某些代码在 Python2 中是确定性的,而在 Python 3 中是非确定性的?

在动态链接库 Anaconda3\Library\bin\mkl_intel_thread.dll 中找不到序数 242