在列中留下具有赋值的行 [英] Leaving rows with a giving value in column

查看:49
本文介绍了在列中留下具有赋值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新时间:

在我的数据集中,我有3列(x,y)和VALUE。
看起来像这样(已经排序):

UPDATED:
In my dataset I have 3 columns (x,y) and VALUE. It's looking like this(sorted already):

df1:
x , y ,value
1 , 1 , 12
2 , 2 , 12
4 , 3 , 12
1 , 1 , 11
2 , 2 , 11
4 , 3 , 11
1 , 1 , 33
2 , 2 , 33
4 , 3 , 33

我需要得到那些行,它们之间的距离(在X和Y列中)是< = 1,可以说它是我的半径。但是在同一时间,我只需要对值相等的那些分组和过滤。
我很难在一个数据集中比较它,因为有一个标头,所以我用python命令创建了第二个数据集:

I need to get those rows where, distance bewteen them (in X and Y column) is <= 1 , lets say its my radius. But in same time i need to group and filter only those where Value is equal. I had problems to compare it in one dataset because there was one header, so i have created second dataset with python commands:

df:
x , y ,value
1 , 1 , 12
2 , 2 , 12
4 , 3 , 12
x , y ,value
1 , 1 , 11
2 , 2 , 11
4 , 3 , 11
x , y ,value
1 , 1 , 33
2 , 2 , 33
4 , 3 , 33

我尝试使用此代码:

def dist_value_comp(row):                         
x_dist = abs(df['y'] - row['y']) <= 1
y_dist = abs(df['x'] - row['x']) <= 1
xy_dist = x_dist & y_dist
max_value = df.loc[xy_dist, 'value'].max()
return row['value'] == max_value

df['keep_row'] = df.apply(dist_value_comp, axis=1)
df.loc[df['keep_row'], ['x', 'y', 'value']]

filtered_df = df[df.apply(lambda line: abs(line['x']- line['y']) <= 1, 1)]
for i in filtered_df.groupby('value'):
     print(i)

在我收到与错误数据帧有关的错误之前,我已经对其进行了修复,但是我仍然没有输出结果。
这就是我从df1创建新数据框df的方式,如果您有更好的主意,请放在此处,因为我总是会打印表格,这是一个很大的缺点。然后再次测试,此def给我空的DataFrame。

Before I have received errors connected with bad data frame, I have repaired it but I have still no results on output. That's how I am creating my new data frame df from df1, if you will have any better idea please put it here, is one have big minus because always prints me the table. And I test it again and this def gives me empty DataFrame.

VALUE1= df1.VALUE.unique()
def separator():
    lst=[]
    for VALUE in VALUE1:
        abc= df1[df1.VALUE==VALUE]
        print abc
    return lst

ab=separator()
df=pd.DataFrame(ab)

当我尝试使用普通数据集df1时,我在输出所有数据时都没有考虑半径= 1

When I am trying normal dataset df1, I have on output all data without taking into account radius =1

我需要像这样一个在输出表上:

I need to get on my output table like this one:

x , y ,value
1 , 1 , 12
2 , 2 , 12
x , y ,value
1 , 1 , 11
2 , 2 , 11
x , y ,value
1 , 1 , 33
2 , 2 , 33

更新2:

我在工作现在使用以下代码:

I am working right now with this code:

filtered_df = df[df.apply(lambda line: abs(line['x']- line['y']) <= 1, 1)]
for i in filtered_df.groupby('value'):
 print(i)

似乎是b好的(我将df1作为输入),但是当我查看输出时,
却无所作为,因为他不知道从哪个值中应该使用半径+/- 1,这就是我认为的原因。
在我的数据集中,我有更多列,因此请考虑我的第4列和第5列 D和 E,因此将从该行中获取半径,其中D列和D列中的最小值

It seems to be ok(i am taking df1 as input), but when i am looking on the output, its doing nothing because he dont know from what value it should use the radius +/-1, thats the reason i think. In my dataset i have more columns, so lets take into account my 4th and 5th column 'D'&'E', so radius will be taken from this row where is minimum value in column D & E in same time.

df1:
x , y ,value ,D ,E
1 , 1 , 12 , 1 , 2
2 , 2 , 12 , 2 , 3
4 , 3 , 12 , 3 , 4
1 , 1 , 11 , 2 , 1
2 , 2 , 11 , 3 , 2
4 , 3 , 11 , 5 , 3
1 , 1 , 33 , 1 , 3
2 , 2 , 33 , 2 , 3
4 , 3 , 33 , 3 , 3

所以输出结果应该与我想要的相同,但现在我知道在这种情况下应从哪个值半径+/- 1开始。
现在有人可以帮助我吗?
抱歉造成误会!

So output result should be same as i want to , but right now i know from what value radius +/-1 in this case should start. Anyone can help me right now? Sorry for misunderstanding !

推荐答案

据我了解,您进行操作的顺序(用距离< = 1并将它们分组)并不重要。

From what I understand, the order in which you make your operations (filter those with distance <= 1 and grouping them) has no importance.

这是我的看法:

#first selection of the lines with right distance
filtered_df = df[df.apply(lambda line: abs(line['x']- line['y']) <= 1, 1)]

# Then group
for i in filtered_df.groupby('value'):
     print(i)
     # Or do whatever you want

让我知道您是否需要解释部分代码的工作原理。

Let me know if you want some explanations on how some part of the code works.

这篇关于在列中留下具有赋值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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