pandas 遍历列以查找文本匹配项,一旦找到,比较两个数据框中的相邻行值 <>

尝试学习在 pandas 中迭代或循环遍历列的方法。在 vba 中,这是一个 for 循环,然后从选定的单元格位置选择偏移量,只有一个选项。但是,我来这里是为了学习熊猫,并且很难理解在比较下一列与右侧相邻或两列相邻时如何保持行的直线。也许可以这样说的另一种方式。一旦在其他数据框 mtype 列中找到 ttype 列文本,我想将两个数据框中的相邻值相互比较。

我附上了用于测试的数据框。我不确定 for 循环是否是实现此目的的最佳方法,但我已经开始了。我读到 pandas 一次处理整个专栏的效率更高。不确定是否可以在这里完成。我的前 3 行代码(2 个 for 循环和 if 语句)正在运行。它循环遍历文本并找到匹配项。但我正在努力处理邻接值。我已经阅读了 iloc 和 loc 语句,因为我觉得它们抢占了行。但我不确定语法。我什至不确定我是否可以提出正确的问题来让我到达我需要的地方,以便我可以学习。因此,如果您能帮助指导我了解这方面的任何阅读材料,我们将不胜感激。pandas loc vs. iloc vs. ix vs. at vs. iat? 根据另一列获取列值,其中包含 pandas 数据框中的字符串列表

需要什么:对于 toc 数据框,我想循环遍历 ttype 列中的每个值,如果 moc 数据框 mtype 列中存在值,则比较 toc[ta 列值] < moc[ma 列值],如果为真,则继续,如果为假则 toc[outfilter] == '1'。

import pandas as pd

from pandas import DataFrame, Series

import numpy as np


toc = {'ttype':['ta1k', 'brek', 'sjfgd',

           'gru2d','brek','crhe','ta1k','jump4'],

       'ta':[1, 2, 9, 9, 2, 2, 1, 1],

       'tc':[0, 1, 0, 0, 1, 0, 2, 0],

       'outfilter':[0, 0, 0, 0,0, 0, 0, 0]}


toc = pd.DataFrame(toc)


moc = {'mtype':[ 'sjfgd','ta1k','gru2d',

            'brek','crhe','jump4'],

       'mo':[2, 2, 4, 4, 3, 4],

       'ma':[2, 2, 4, 4, 2, 3],

       'mc':[1, 1, 3, 3, 1, 1]}


moc = pd.DataFrame(moc)


#-----

for tval in toc['ttype']:                          # Gets toc['ttype'].value

    for mval in moc['mtype']:                      # Gets toc['mtype'].value

        if t == m:                                 # compares if tval == mval

            if toc.loc['ta'] < moc.loc['ma']:      # compares toc.[ta] column value < moc.[ma]

                continue

            else:

                toc.loc['outfilter'] = '1'         # if the above is greater place '1' in outfilter 

                                                   #  column

        else:

            continue

#-----

 print(toc)

 print(moc)


我真的很感谢你们的帮助,我希望有一天我能回报你们的帮助,并将它付诸实践。感谢您的时间。!!!如果您有任何问题,请告诉我。


holdtom
浏览 215回答 1
1回答

神不在的星期二

ttype我会合并和列上的数据框,mtype类似于在 Excel 中执行索引匹配/vlookup,但您不想合并整个moc数据框,因此只需指定并合并您需要的列 (mtype和ma)。从那里,只需执行一个操作np.where以查看ta值是否大于ma值并返回1或0类似于 Excelif公式。最后,删除不需要的列输入:import pandas as pd, numpy as nptoc = {'ttype':['ta1k', 'brek', 'sjfgd',&nbsp; &nbsp; &nbsp; &nbsp;'gru2d','brek','crhe','ta1k','jump4'],&nbsp; &nbsp;'ta':[1, 2, 9, 9, 2, 2, 1, 1],&nbsp; &nbsp;'tc':[0, 1, 0, 0, 1, 0, 2, 0],&nbsp; &nbsp;'outfilter':[0, 0, 0, 0,0, 0, 0, 0]}toc = pd.DataFrame(toc)moc = {'mtype':[ 'sjfgd','ta1k','gru2d',&nbsp; &nbsp; &nbsp; &nbsp; 'brek','crhe','jump4'],&nbsp; &nbsp;'mo':[2, 2, 4, 4, 3, 4],&nbsp; &nbsp;'ma':[2, 2, 4, 4, 2, 3],&nbsp; &nbsp;'mc':[1, 1, 3, 3, 1, 1]}moc = pd.DataFrame(moc)代码:toc = pd.merge(toc,moc[['mtype','ma']],how='left',left_on='ttype',right_on='mtype')toc['outfilter'] = np.where((toc['ta'] > toc['ma']),1,0)toc = toc.drop(['mtype','ma'], axis=1)toc逐行分解代码:步骤 1(类似于 excelindex-match公式):pd.merge(toc,moc[['mtype','ma']],how='left',left_on='ttype',right_on='mtype')&nbsp; &nbsp;ttype&nbsp; ta&nbsp; tc&nbsp; outfilter&nbsp; mtype&nbsp; ma0&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;ta1k&nbsp; &nbsp;21&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;brek&nbsp; &nbsp;42&nbsp; sjfgd&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; sjfgd&nbsp; &nbsp;23&nbsp; gru2d&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; gru2d&nbsp; &nbsp;44&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;brek&nbsp; &nbsp;45&nbsp; &nbsp;crhe&nbsp; &nbsp;2&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;crhe&nbsp; &nbsp;26&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;ta1k&nbsp; &nbsp;27&nbsp; jump4&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; jump4&nbsp; &nbsp;3第 2 步(类似于 excelIF公式):toc['outfilter'] = np.where((toc['ta'] > toc['ma']),1,0)&nbsp; &nbsp; ttype&nbsp; ta&nbsp; tc&nbsp; outfilter&nbsp; mtype&nbsp; ma0&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;ta1k&nbsp; &nbsp;21&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;brek&nbsp; &nbsp;42&nbsp; sjfgd&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; sjfgd&nbsp; &nbsp;23&nbsp; gru2d&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; gru2d&nbsp; &nbsp;44&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;brek&nbsp; &nbsp;45&nbsp; &nbsp;crhe&nbsp; &nbsp;2&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;crhe&nbsp; &nbsp;26&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;ta1k&nbsp; &nbsp;27&nbsp; jump4&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; jump4&nbsp; &nbsp;3第 3 步 - 最终输出(仅删除不需要的列):toc = toc.drop(['mtype','ma'], axis=1)&nbsp; &nbsp;ttype&nbsp; ta&nbsp; tc&nbsp; outfilter0&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 01&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 02&nbsp; sjfgd&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 13&nbsp; gru2d&nbsp; &nbsp;9&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 14&nbsp; &nbsp;brek&nbsp; &nbsp;2&nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 05&nbsp; &nbsp;crhe&nbsp; &nbsp;2&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 06&nbsp; &nbsp;ta1k&nbsp; &nbsp;1&nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 07&nbsp; jump4&nbsp; &nbsp;1&nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0如果我多想一点,可能有一种更简单的方法可以在 python 中使用 pandas 方法仅使用一行 cod 来执行此操作,但这种方法足够简单且易于理解。此外,VBA 也是我大约 18 个月前从 Pandas 切换到的语言。我会说 99% 的问题都可以用 pandas 方法、列表理解或.apply(lambda x:.... Pandas 方法或 numpy 方法在简单性、速度、性能等方面始终是可行的方法。在 VBA 中循环非常流行,但您应该尽快摆脱它并学习各种 pandas方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python