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

2024-05-14 18:12:24 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试学习如何迭代或循环浏览pandas中的列。在vba中,这是一个for循环,然后选择“从选定单元格位置偏移”,只有一个选项。然而,我在这里学习pandas,并且很难理解在比较下一列邻接与右侧或两列的邻接时如何保持行的笔直。也许还有另一种说法。一旦在另一个dataframe mtype列中找到了ttype列文本,我想比较两个dataframe中的相邻值

我已附加数据帧进行测试。我不确定for循环是否是实现这一点的最佳方式,但我已经开始了。我读到熊猫在一次处理整个专栏时效率更高。我不确定这里是否能做到。我的前3行代码(2行用于循环和if语句)正在运行。它循环遍历文本并找到匹配项。但我正在努力处理邻接值。我已经阅读了iloc和loc声明,因为我觉得他们抓住了这一行。但我不确定语法。我甚至不确定我能否提出正确的问题,让我去我需要的地方学习。因此,任何你能帮助我了解这方面的阅读材料都将不胜感激pandas loc vs. iloc vs. ix vs. at vs. iat?get column value based on another column with list of strings in pandas dataframe

所需内容:对于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)


 What I would like to do:  The '1's located in the outfilter column are a result of the toc-df[ta 
 column value] being greater than moc-df[ma column value]. 

 toc-df  ttype   ta   tc   outfilter
     0   ta1k    1    0    0
     1   brek    2    1    0
     2   sjfgd   9    0    1
     3   gru2d   9    0    1
     4   brek    2    1    0
     5   crhe    2    0    0
     6   ta1k    1    2    0
     7   jump4   1    0    0

我真的很感谢你们的帮助,我希望有一天我能报答你们的帮助并把它提前支付。谢谢你抽出时间来。!!!如果你有任何问题,请告诉我


Tags: inpandasifvaluecolumnloctama
1条回答
网友
1楼 · 发布于 2024-05-14 18:12:24
  1. 我将合并ttypemtype列上的数据帧,类似于在Excel中进行索引匹配/vlookup,但您不希望合并整个moc数据帧,所以只需指定并合并所需的列(mtypema
  2. 从这里开始,只需执行np.where以查看ta值是否大于ma值,并返回10类似于Excel if公式
  3. 最后,删除不需要的列

输入:

import pandas as pd, 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)

代码:

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(类似于excel index-match公式):

pd.merge(toc,moc[['mtype','ma']],how='left',left_on='ttype',right_on='mtype')

   ttype  ta  tc  outfilter  mtype  ma
0   ta1k   1   0          0   ta1k   2
1   brek   2   1          0   brek   4
2  sjfgd   9   0          0  sjfgd   2
3  gru2d   9   0          0  gru2d   4
4   brek   2   1          0   brek   4
5   crhe   2   0          0   crhe   2
6   ta1k   1   2          0   ta1k   2
7  jump4   1   0          0  jump4   3

步骤2(类似于excel IF公式):

toc['outfilter'] = np.where((toc['ta'] > toc['ma']),1,0)

    ttype  ta  tc  outfilter  mtype  ma
0   ta1k   1   0          0   ta1k   2
1   brek   2   1          0   brek   4
2  sjfgd   9   0          1  sjfgd   2
3  gru2d   9   0          1  gru2d   4
4   brek   2   1          0   brek   4
5   crhe   2   0          0   crhe   2
6   ta1k   1   2          0   ta1k   2
7  jump4   1   0          0  jump4   3

步骤3-最终输出(仅删除不需要的列):

toc = toc.drop(['mtype','ma'], axis=1)

   ttype  ta  tc  outfilter
0   ta1k   1   0          0
1   brek   2   1          0
2  sjfgd   9   0          1
3  gru2d   9   0          1
4   brek   2   1          0
5   crhe   2   0          0
6   ta1k   1   2          0
7  jump4   1   0          0

如果我再多想一想的话,在python中可能有一种更简单的方法可以做到这一点,只需要使用pandas方法的一行cod,但是这种方法足够简单并且易于理解

另外,VBA也是大约18个月前我从熊猫那里换来的语言。我想说99%的问题可以通过熊猫方法、列表理解或.apply(lambda x:...解决。Pandas方法或numpy方法在简单性、速度、性能等方面始终是一种可行的方法。在VBA中循环是非常流行的,但您应该尝试尽快摆脱这种情况,并学习各种Pandas方法

相关问题 更多 >

    热门问题