按列值选择

墙壁

我知道在 arcpy 中是可能的。找出是否可以在熊猫中发生。

我有以下

data= {'Species':[ 'P.PIN','P.PIN','V.FOG', 'V.KOP', 'E.MON', 'E.CLA', 'E.KLI', 'D.FGH','W.ERT','S.MIX','P.PIN'], 
  'FY':[ '2002','2016','2018','2010','2009','2019','2017','2016','2018','2018','2016']}

我需要选择 FY 等于或早于 2016 年的所有 P.PIN、P.RAD 和任何其他以 E 开头的物种,并将其放入新的数据框中。

我怎样才能做到这一点。我只能选择 P.PIN 和 P.RAD,但添加了所有其他以 E 开头的选项;

df3 =df[(df['FY']>=2016)&(df1['LastSpecies'].isin(['P.PIN','P.RAD']))]

您的帮助将不胜感激。

循序渐进。但是你也可以结合里面的逻辑np.where()只是想显示所有条件都完成了。

首先将您的df['FY']类型转换为 int,以便我们可以使用大于 (>) 运算符。

>>> df['FY'] = df['FY'].astype(int)

>>> df['flag'] = np.where(df['Species'].isin(['P.PIN', 'P.RAD']), ['Take'], ['Remove'])
>>> df
   Species    FY    flag
0    P.PIN  2002    Take
1    P.PIN  2016    Take
2    V.FOG  2018  Remove
3    V.KOP  2010  Remove
4    E.MON  2009  Remove
5    E.CLA  2019  Remove
6    E.KLI  2017  Remove
7    D.FGH  2016  Remove
8    W.ERT  2018  Remove
9    S.MIX  2018  Remove
10   P.PIN  2016    Take

>>> df['flag'] = np.where((df['FY'] > 2016) & (df['Species'].str.startswith('E')), ['Take'], df['flag'])
>>> df
   Species    FY    flag
0    P.PIN  2002    Take
1    P.PIN  2016    Take
2    V.FOG  2018  Remove
3    V.KOP  2010  Remove
4    E.MON  2009  Remove
5    E.CLA  2019    Take
6    E.KLI  2017    Take
7    D.FGH  2016  Remove
8    W.ERT  2018  Remove
9    S.MIX  2018  Remove
10   P.PIN  2016    Take

>>> new_df = df[df['flag'].isin(['Take'])][['Species', 'FY']]
>>> new_df
   Species    FY
0    P.PIN  2002
1    P.PIN  2016
5    E.CLA  2019
6    E.KLI  2017
10   P.PIN  2016

希望这会有所帮助:D

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章