pandas 中的isin()方法忽略重复值.我们如何预防呢? [英] isin() method in pandas ignoring the duplicate values. how can we prevent that?

查看:58
本文介绍了 pandas 中的isin()方法忽略重复值.我们如何预防呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从无法在此处显示的数据帧中获取所有值,但是代码中提到的所有列名都存在于此数据帧中.因为,我正在使用isin()方法从列中获取值. isin()方法不会为您提供重复的值,但我也希望重复的值.在上面的代码中,我展示了如何使用isin()方法从多列中获取多个值.在voltage_values变量中,我使用了isin()方法,该方法不会获取重复值.我该怎么做不会删除重复值.

I am fetching all the values from the dataframes I cant show it here but all the column names that mentioned in the code exist in this dataframe. Since, I am using isin() method to fetch the values from the column. isin() method does not gives you duplicate values but I want the duplicate values as well. In the above code i shows that how I used isin() method to fetch the multiple values from multiple column. In the voltage_values variable i used isin() method which not fetching the duplicate values.What Can i do that does not remove duplicate values.

start_values = [1,2,3]
load_value_name = [f"^I__ND_LD({n})" for n in start_values]
load_values=df[df['I__ND_LD'].isin(load_value_name)]['I__ND_LD_Values'].values.astype(np.int)
print(load_values)
bus_names = [f"^I__BS_ND({n})" for n in load_values]
print(bus_names)
bus_values = df[df['I__BS_ND'].isin(bus_names)]['I__BS_ND_Values'].values.astype(np.int)
print(bus_values)
voltage_bus_value = [f"^VMEAS_BS({n})" for n in bus_values]
print(voltage_bus_value)
voltage_values = df[df['VMEAS_BS'].isin(voltage_bus_value)]['VMEAS_BS_Values'].reindex().values
print(voltage_values)

上面显示了相应的输出

load_values=[10 45 44]
bus_names=['^I__BS_ND(10)', '^I__BS_ND(45)', '^I__BS_ND(44)']
bus_values=[ 5 17 17]
voltage_bus_value=['^VMEAS_BS(5)', '^VMEAS_BS(17)', '^VMEAS_BS(17)']
voltage_values=[0.9908185  0.99612296]

我们可以看到"^ VMEAS_BS(17)"出现了两次,但是在数组中,我仅得到一个值为0.99612296的值,但我希望该值为两次.可能的解决方案是什么.

As we can see that "^VMEAS_BS(17)" came two times but in the array I got only only one value which is 0.99612296 but I want this value two times. What could be the possible solution for that.

推荐答案

我的数据帧中没有VMEAS_BS,所以我将向您显示带有load_values的结果.

I don't have VMEAS_BS in my dataframe, so I'll show you the result with load_values.

这是我能想到的最快的

替代

voltage_values = df[df['VMEAS_BS'].isin(voltage_bus_value)]['VMEAS_BS_Values'].reindex().values

所有这些行(对不起!)

with all these lines (sorry!!)

voltage_values = []
for _,value in enumerate(voltage_values):
  voltage_values.extend(df[df['VMEAS_BS'] == value]['VMEAS_BS_Values'].reindex().values)

这是一个带有load_values的示例

Here is an example with load_values

load_values = []
for _,value in enumerate(load_value_name):
  load_values.extend(df[df['I__ND_LD']== value]['I__ND_LD_Values'].values.astype(np.int))
#output
[10, 45, 44]

这篇关于 pandas 中的isin()方法忽略重复值.我们如何预防呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆