【发布时间】:2018-10-13 10:38:43
【问题描述】:
import random
import re
import pandas as pd
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)
def check_for_greet():
while True:
try:
sentence = input("Start chatting: ")
wordList = re.sub("[^\w]", " ", sentence).split()
if sentence == "$$$":
return "END"
for word in wordList:
for col in df3.columns:
if word.lower() in df3[col].values:
print (df3[col][0])
break
以上内容现在在列之间完美运行(谢谢@R.yan),但唯一的问题是当我输入“hi hi”时,它会打印两次:
Start chatting: hi hi
Greetings to you too
Greetings to you too
为什么会这样,我打破了for循环,继续返回while循环?!
【问题讨论】:
标签: python pandas loops dataframe slice