Python数据帧循环迭代

Python数据帧循环迭代,python,pandas,for-loop,iteration,Python,Pandas,For Loop,Iteration,这给了我一个输出: import pandas as pd import re df1 = pd.DataFrame({"Greetings": ["Greetings to you too", "hi", "hello", "hey", "greetings", "sup", "what's up", "yo"]}) df2 = pd.DataFrame({"Farewell": ["GoodBye", "See you", "Bye", "Laters"]}) frames = [df1

这给了我一个输出:

import pandas as pd
import re

df1 = pd.DataFrame({"Greetings": ["Greetings to you too", "hi", "hello", "hey", "greetings", "sup", "what's up", "yo"]})
df2 = pd.DataFrame({"Farewell": ["GoodBye", "See you", "Bye", "Laters"]})

frames = [df1, df2]
df3=pd.concat(frames, axis=1, join='outer', copy=True)


sentence = 'hi'
for index, row in df3.iterrows():
   if index>0:
       if sentence.lower() in df3.iloc[index,:].values:
           print(df3.iloc[index].values)
最终目的基本上是从第二行开始遍历所有列以查找关键字匹配(这就是为什么我将:if index>0)并且如果它找到了匹配项,则默认情况下输出该列的第一项(在本例中为“Hellodes to you too”,在本场景中为df3.iloc[0,0]

因此,我需要帮助添加额外的代码行,以便运行代码后的结果输出如下所示:

['hi' 'See you']

有人能帮忙吗。

我终于得到了答案,代码只是需要调整一下:

"Greetings to you too"

您的描述非常混乱。请提供一个您期望结果的示例。我编辑了这篇文章,希望它能让您更容易理解。我只需要最终输出来显示“也向您致意”。我不理解您将最后一条打印语句从
print(df3.iloc[index].values)
更改为
print(df3.iloc[:index].values)
,然后将打印
[[['也向您问好'再见]]]
。问题是df3.iloc第一个索引对行进行操作,而第二个索引对列进行操作。@prabhakar,该建议输出该索引中所有行的值,我只需要输出“也向您问好”。
for index, row in df3.iterrows():
            if index>0:
                if sentence.lower() in df3.iloc[index,:].values:
                    print((df3.iloc[:index]).iloc[0,0])