简体   繁体   English

如何匹配熊猫数据框同一列中的两个值

[英]how to match two values in the same column of a pandas dataframe

I have two data frames as below 我有以下两个数据框

PD  106352  00253   01-02-2018  0.73
PD  108181  00253   20-12-2017  13.91
PD  108222  00253   01-08-2017  -2,227.50
PD  108224  00253   01-08-2017  -4,455.00
PD  108848  00253   25-07-2017  -2,342.13
PD  108852  00253   25-06-2018  1,764.16
PD  108860  00253   12-07-2017  -3,144.81
PD  108871  00253   01-07-2017  -144.17
PD  109455  00253   01-07-2017  -271.25
PD  109472  00253   04-07-2017  -389.00

and

PV  73006   00253   01-09-2017  16,956.25
PV  73006   00253   01-09-2017  2,227.50
PV  73006   00253   01-09-2017  2,227.50
PV  75499   00253   01-07-2017  30,351.42
PV  75645   00253   03-07-2017  34,468.29
PV  82899   00253   12-12-2017  2,342.40

I tried making a list of of the fifth column of both dataframes, compare them, if match found take out the index and used loc to set the result column.but no successfull. 我尝试制作两个数据帧第五列的列表,将它们进行比较,如果找到匹配项,则取出索引并使用loc设置结果列。但是没有成功。

I want to compare the 5th column of both dataframes and match the absolute value ignoring the sign and if 1:1 match found i want to add a column and comment it as nill and if 1:n matches found i want to comment only 1:1 out of them as nill and leave others in the result column as blank 我想比较两个数据帧的第五列,并忽略符号匹配绝对值,如果找到1:1匹配,我想添加一列并将其注释为nill,如果找到1:n匹配,我只想注释1:其中1个为nill,结果栏中的其他空白

I want to do something like below 我想做下面的事情

PD  108222  00253   01-08-2017  -2,227.50 Nill
PV  73006   00253   01-09-2017  2,227.50  Nill
PV  73006   00253   01-09-2017  2,227.50

Please look at the below code, this is something which i could come up really quick, i think it should solve your problem. 请看下面的代码,这是我可以很快提出的,我认为它应该可以解决您的问题。

import pandas as pd

#creating data
data_a = pd.read_csv('data_a.csv', sep=',', header=None)
data_a[4]=data_a[4].abs()
data_b = pd.read_csv('data_b.csv', sep=',', header=None)

#converting to list
a=data_a[4].tolist()
b=data_b[4].tolist()


#Removing duplicates and preserving the order so you get 1:1 and not 1:N
b1=[el for i, el in enumerate(b) if el not in b[:i]]

#getting indices of matching values in tow datasets
abc=[i for i, item in enumerate(a) if item in b1]
deg=[i for i, item in enumerate(b1) if item in a]

#Creating blank new column
data_a[5]=''
data_b[5]=''

#Filling matching locations with Nill
data_a.iloc[abc,5] = 'Nill'
data_b.iloc[deg,5] = 'Nill'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何切换同一Pandas DataFrame中的列值 - How to switch column values in the same Pandas DataFrame 如何创建另一列,其中包含基于 Pandas 数据框中同一分类列的两个不同值的操作? - How to create another column that contains an operation based on two different values of a same categorical column in a pandas dataframe? Pandas 将 dataframe 的列重命名为另一个 dataframe 的值,如果两个 Z6A8064B53C47945557755705 列的值匹配 - Pandas rename column of dataframe to value of another dataframe if values of two dataframe columns match 如何合并来自同一行和列索引/值的两个熊猫数据框的值? - How can I merge the values from two pandas dataframe which as same row and column indexes/value? 如何在 pandas Dataframe 中使用具有列值的行来匹配行和过滤 - How to match rows and filtering using rows with column values in pandas Dataframe 如何通过 pandas 中的相同列 ID 从 dataframe 中的两个不同分类列值创建新列? - How do you create new column from two distinct categorical column values in a dataframe by same column ID in pandas? pandas dataframe - 两列字符串匹配和组 - pandas dataframe - two column string match and group 如果两个单元格值与 pandas 中的另一个较小子集 dataframe 匹配,则使用 True 填充新的 dataframe 列 - Populate a new dataframe column with True if two cell values match another smaller subset dataframe in pandas 如何在同一只熊猫数据框的一列中执行两项聚合操作? - How to perform two aggregate operations in one column of same pandas dataframe? pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM