如何仅获取熊猫数据框中具有给定值(或一组值)的行*和仅*列

时间:2020-10-29 18:42:59

标签: python pandas dataframe select indexing

我只想查找在给定值集中具有字段值的行和列

我可以限制行数,从而获得行数。

说我有这个数据框:

print(df)

# year     1970  1971  1972  1973  1974  1975  1976  1977  1978
# country  
# Malawi    NaN   NaN   NaN   123   NaN   234   NaN   NaN   NaN
# OtherC    NaN   NaN   NaN   124   NaN   234   NaN   NaN   NaN
# OtherD    NaN   NaN   NaN   124   NaN   235   NaN   NaN   NaN

我要返回的是包含123或234的行和列:

# year     1973  1975
# country  
# Malawi    123   234
# OtherC    124   234

我可以这样做,只返回具有给定值的行,而不选择列:

print(df[df.isin([123, 234]).any(axis=1)])

# year     1970  1971  1972  1973  1974  1975  1976  1977  1978
# country  
# Malawi    NaN   NaN   NaN   123   NaN   234   NaN   NaN   NaN
# OtherC    NaN   NaN   NaN   124   NaN   234   NaN   NaN   NaN

但是,当我尝试这两个语句中的任何一个时,都会出现错误:

print(df[df.isin([123, 234]).any(axis=1)]\
[df.isin([123, 234]).any(axis=0)])

print(df[df.isin([123, 234]).any(axis=0)]\
[df.isin([123, 234]).any(axis=1)])

...
IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

或者,这(包含实际数据集):

print(df[[df.isin([123, 234]).any(axis=1)],\
[df.isin([123, 234]).any(axis=0)]])

# The following output is from using the real dataset, so it
# includes more rows and columns, and the sought-after values
# are in different locations, but you get the idea:

# TypeError: '([country
# Afghanistan            False
# Albania                False
# Andorra                False
# Angola                 False
# Antigua and Barbuda    False
#                        ...  
# Uruguay                False
# Vanuatu                False
# Venezuela              False
# Vietnam                False
# Zimbabwe               False
# Length: 160, dtype: bool], [year
# 1970    False
# 1971    False
# 1972    False
# 1973    False
# 1974    False
# 1975    False
# 1976    False
# 1977    False
# 1978    False
# 1979    False
# 1980    False
# 1981    False
# 1982    False
# 1983    False
# 1984    False
# 1985    False
# 1986    False
# 1987    False
# 1988    False
# 1989    False
# 1990    False
# 1991    False
# 1992    False
# 1993    False
# 1994    False
# 1995    False
# 1996    False
# 1997    False
# 1998    False
# 1999     True
# 2000     True
# 2001    False
# 2002    False
# 2003    False
# 2004    False
# 2005    False
# 2006    False
# 2007    False
# 2008    False
# 2009    False
# 2010    False
# 2011    False
# 2012    False
# 2013    False
# 2014    False
# 2015    False
# 2016    False
# 2017    False
# 2018    False
# dtype: bool])' is an invalid key

1 个答案:

答案 0 :(得分:3)

您用any(axis=1)获取行,并用any(axis=0)获取列。请记住,尽可能避免索引链接(例如df[][]

valids = df.isin([123,234])
df.loc[valids.any(axis=1), valids.any(axis=0)]

输出:

year     1973  1975
country            
Malawi    123   234
OtherC    124   234