Python Pandas从其他列返回值

时间:2018-10-10 12:19:46

标签: python pandas dataframe

我有一个文件“ specieslist.txt”,其中包含以下信息:

Bacillus,genus
Borrelia,genus
Burkholderia,genus
Campylobacter,genus

现在,我希望python在第一列(在此示例中为“ Campylobacter”)中寻找一个变量,并返回第二列的值(“ genus”)。我写了以下代码

import csv
import pandas as pd

species_import = 'Campylobacter'
df = pd.read_csv('specieslist.txt', header=None, names = ['species', 'level'] )
input = df.loc[df['species'] == species_import]
print (input['level'])

但是,我的代码返回太多,而我只想要“属”

3    genus
Name: level, dtype: object

4 个答案:

答案 0 :(得分:4)

您可以按iat选择系列的第一个值:

species_import = 'Campylobacter'
out = df.loc[df['species'] == species_import, 'level'].iat[0]
#alternative
#out = df.loc[df['species'] == species_import, 'level'].values[0]
print (out)
genus

如果没有匹配的值并且返回empty Series,则更好的解决方案有效-它返回no match

  

@jpp评论
  仅当您的序列较大且匹配值应位于顶部附近时,此解决方案才更好。

species_import = 'Campylobacter'
out = next(iter(df.loc[df['species'] == species_import, 'level']), 'no match')
print (out)
genus

编辑:

评论中的想法,感谢@jpp:

def get_first_val(val):
    try:
        return df.loc[df['species'] == val, 'level'].iat[0]
    except IndexError:
        return 'no match'

print (get_first_val(species_import))
genus

print (get_first_val('aaa'))
no match

编辑:

df = pd.DataFrame({'species':['a'] * 10000 + ['b'], 'level':np.arange(10001)})

def get_first_val(val):
    try:
        return df.loc[df['species'] == val, 'level'].iat[0]
    except IndexError:
        return 'no match'


In [232]: %timeit next(iter(df.loc[df['species'] == 'a', 'level']), 'no match')
1.3 ms ± 33.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [233]: %timeit (get_first_val('a'))
1.1 ms ± 21 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)



In [235]: %timeit (get_first_val('b'))
1.48 ms ± 206 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [236]: %timeit next(iter(df.loc[df['species'] == 'b', 'level']), 'no match')
1.24 ms ± 10.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

答案 1 :(得分:3)

各种方法的性能,以证明何时使用next(...)是有用的。

n = 10**6
df = pd.DataFrame({'species': ['b']+['a']*n, 'level': np.arange(n+1)})

def get_first_val(val):
    try:
        return df.loc[df['species'] == val, 'level'].iat[0]
    except IndexError:
        return 'no match'

%timeit next(iter(df.loc[df['species'] == 'b', 'level']), 'no match')     # 123 ms per loop
%timeit get_first_val('b')                                                # 125 ms per loop
%timeit next(idx for idx, val in enumerate(df['species']) if val == 'b')  # 20.3 µs per loop

答案 2 :(得分:2)

get

使用pandas.Series.get,如果'species'是唯一的,则可以返回标量值;如果不是唯一的,则可以返回pandas.Series

f = df.set_index('species').level.get

f('Campylobacter')

'genus'

如果不在数据中,则可以提供默认值

f('X', 'Not In Data')

'Not In Data'

我们也可以使用dict.get并且只返回标量。如果不是唯一的,将返回最后一个。

f = dict(zip(df.species, df.level)).get

如果您想返回第一个,可以通过几种方法完成

f = dict(zip(df.species[::-1], df.level[::-1])).get 

f = df.drop_duplicates('species').pipe(
    lambda d: dict(zip(d.species, d.level)).get
)

答案 3 :(得分:0)

# Change the last line of your code to 
print(input['level'].values) 
# For Explanation refer below code

import csv
import pandas as pd

species_import = 'Campylobacter'
df = pd.read_csv('specieslist.txt', header=None, names = ['species', 'level'] )

input = df['species'] == species_import # return a pandas dataFrame

print(type(df[input])) # return a Pandas DataFrame

print(type(df[input]['level'])) # return a Pandas Series 

# To obtain the value from this Series.
print(df[input]['level'].values)  # return 'genus'