Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回条件值_Python_Pandas - Fatal编程技术网

Python 返回条件值

Python 返回条件值,python,pandas,Python,Pandas,只是需要一点帮助,从数据帧返回一些值 我有一个数据帧(称为df1),其中包含一些值: ID X Y Distance Date 1 1 2 2.2 01/01/2000 2 2 3 1.8 02/02/2001 3 3 4 1.2 03/03/2002 4 4 5 2.7 04/04/2003 5 5

只是需要一点帮助,从数据帧返回一些值


我有一个数据帧(称为df1),其中包含一些值:

ID      X    Y    Distance   Date
1       1    2    2.2        01/01/2000
2       2    3    1.8        02/02/2001
3       3    4    1.2        03/03/2002
4       4    5    2.7        04/04/2003 
5       5    6    3.8        05/05/2004
目前,我有一个代码,它创建了一个新的列-df1['injust 2k']——如果距离在2km以内,它将返回True。例如,这看起来像:

df1['Within 2k'] = df1['distance'] <= 2
print("df1")

ID      X    Y    Distance   Date         Within 2k
1       1    2    2.2        01/01/2000   False
2       2    3    1.8        02/02/2001   True
3       3    4    1.2        03/03/2002   True
4       4    5    2.7        04/04/2003   False
5       5    6    3.8        05/05/2004   False
我需要一些代码,它将:

1) 返回距离小于2的第一个ID和距离


2) 如果表中的每个值都在距离2km之外,则返回ID和距离的字符串“Null”。

实际上,您不需要额外的列:

In [35]: df
Out[35]:
   ID  X  Y  Distance       Date
0   1  1  2       2.2 2000-01-01
1   2  2  3       1.8 2001-02-02
2   3  3  4       1.2 2002-03-03
3   4  4  5       2.7 2003-04-04
4   5  5  6       3.8 2004-05-05

In [36]: df.loc[df['Distance'] <= 2].nsmallest(1, 'Date')[['ID','Distance']]
Out[36]:
   ID  Distance
1   2       1.8
[35]中的
:df
出[35]:
ID X Y距离日期
0   1  1  2       2.2 2000-01-01
1   2  2  3       1.8 2001-02-02
2   3  3  4       1.2 2002-03-03
3   4  4  5       2.7 2003-04-04
4   5  5  6       3.8 2004-05-05

在[36]:df.loc[df['Distance']中,如果df1中的每个值都在距离之外,我该怎么办?@CTaylor19,如果您的行数在2km以内,那么您希望的输出格式是什么?就像“Null”之类的东西,或者当我进一步操作数据时很容易注意到的任何东西,这段代码(我正在另一个数据帧上应用)导致许多“空数据帧”
Site2km = df1.loc[df1['Date'].idxmin(),'ID']
Dist2km = df1.loc[df1['Date'].idxmin(),'Distance']

return pd.Series([Site2km, Dist2km])
In [35]: df
Out[35]:
   ID  X  Y  Distance       Date
0   1  1  2       2.2 2000-01-01
1   2  2  3       1.8 2001-02-02
2   3  3  4       1.2 2002-03-03
3   4  4  5       2.7 2003-04-04
4   5  5  6       3.8 2004-05-05

In [36]: df.loc[df['Distance'] <= 2].nsmallest(1, 'Date')[['ID','Distance']]
Out[36]:
   ID  Distance
1   2       1.8
In [47]: df
Out[47]:
   ID  X  Y  Distance       Date
0   1  1  2       2.2 2000-01-01
1   2  2  3       1.8 2001-02-02
2   3  3  4       1.2 2002-03-03
3   4  4  5       2.7 2003-04-04
4   5  5  6       3.8 2004-05-05

In [48]: r = df.loc[df['Distance'] <= 2].nsmallest(1, 'Date')[['ID','Distance']]

In [49]: r
Out[49]:
   ID  Distance
1   2       1.8
In [50]: df.Distance += 10

In [51]: r = df.loc[df['Distance'] <= 2].nsmallest(1, 'Date')[['ID','Distance']]

In [52]: r
Out[52]:
Empty DataFrame
Columns: [ID, Distance]
Index: []

In [53]: if r.empty:
    ...:     r.loc[0] = [np.nan, np.nan]
    ...:

In [54]: r
Out[54]:
   ID  Distance
0 NaN       NaN