如何在熊猫中执行逐行操作

时间:2018-06-20 21:05:50

标签: python string pandas dataframe

我有一个包含两列的数据框,last_namedescription

查看description是否在每一行中包含last_name的优雅方法是什么?

我有一个丑陋的


for i in range(0,len(df0)):
    df0['last_name_position'].loc[i] = \ 
        df0['description'].loc[i].find(df0['last_name'].loc[i])

正在运行。

这基本上与 https://exceljet.net/formula/cell-contains-specific-text

3 个答案:

答案 0 :(得分:3)

使用df.apply可以达到相同的结果:

df0['last_name_position'] = \ 
    df0.apply(lambda x: x.description.find(x.last_name), axis=1)

如果您不需要找到子字符串的索引:

df0['last_name_position'] = \ 
    df0.apply(lambda x: x.last_name in x.description, axis=1)

答案 1 :(得分:0)

更好的方法是使用索引遍历行。然后使用in关键字进行比较:

import pandas as pd 

a = pd.DataFrame([['Smith','Some description'],['Jones','Some Jones description']], columns=['last_name','description'])

for rname in a.index:
    row = a.loc[rname]

    it_contains = row['last_name'] in row['description']
    print "Description contains last name:", it_contains

# Outputs:
# Description contains last name: False
# Description contains last name: True

答案 2 :(得分:0)

保留数据框:

import pandas as pd 

a = pd.DataFrame([['Smith','Some description'],['Jones','Some Jones description']], columns=['last_name','description'])
a['Match']=a.apply(lambda x: x['last_name'] in x['description'], axis=1)
a.head()

last_name   description             Match
0   Smith   Some description        False
1   Jones   Some Jones description  True