简体   繁体   English

迭代熊猫中的行以匹配列中的值

[英]iterating rows in Pandas to match values in columns

I build a dictionary app, where I use a 3 column df1 DataFrame (English - German - Spanish). 我构建了一个字典应用程序,在其中使用了3列df1 DataFrame(英语-德语-西班牙语)。 I need to iterate every column where a user given a value in English, and he should post the equivalent word in Spanish and German. 我需要遍历用户为英语提供值的每一列,并且他应该用西班牙语和德语发布相同的词。

I started the code like that: 我这样启动代码:

for w in df1.index:
  e = df1.loc[w]['English']
  print("Translate: " + e + "in German")
  d = input('')
  print("Now try Spanish\n")
  s = input('')

How to iterate RANDOMLY to check if the supplied input matches the values in a particular row? 如何随机地检查提供的输入是否与特定行中的值匹配?

Update: Data Sample 更新:数据样本

| Deutsch            | Englisch        | Español        |
|--------------------|-----------------|----------------|
| bei meinen eltern  | with my paernts | con mis padres |
| zu Hause           | at home         | en casa        |
| stammen aus        | come from       | viene de       |
| ist in ... geboren | was born in     | nació en       |

If your data set is much larger than the number of rows you actually want to iterate through (which is very possible if you have a full dictionary of words), you can create a randomized sample dataframe by using 如果您的数据集比您实际要遍历的行数大得多(如果您有完整的单词词典,则很有可能),则可以使用以下方法创建随机样本数据框:

df2 = df1.sample(frac = 0.1) # randomized sample, 10% of df1
df2 = df1.sample(frac=1) # randomized copy of all of df1

or 要么

df2 = df1.sample(100) # randomized sample, 10 lines from df1

The first will be relative to the size of your df1 , as defined by the fraction used as a parameter, the second will be 100 rows regardless of the size of df1 . 第一个相对于df1的大小,由用作参数的分数定义,第二个相对于df1的大小为100行。 Both are a randomly selected, randomly ordered sample. 两者都是随机选择的,随机排序的样本。

Secondly, you want to iterate over the rows. 其次,您要遍历行。 You're passing each row to the user sequentially and waiting on input. 您正在按顺序将每一行传递给用户并等待输入。 The following code should work just fine. 以下代码应该可以正常工作。

for idx,row in df2.iterrows(): 
    d = input("Translate: " + row.English + " in German")
    if d==row.German:
        print("Good!") # Or another case for successful translation
    else:
        print("The correct answer was "+row.German) # Or another case for incorrect translation
    s = input("Translate: " + row.English + " in Spanish")
    if s==row.Spanish:
        print("Good!")
    else:
        print("The correct answer was "+row.Spanish)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM