如何根据指定的条件将数据帧划分为新的数据帧?

我有一个数据框:例如:

df =

Questions             Answers

Where is Amazon?       Brazil

Is he a scientist?         No

Did he stole my money?    Yes

What does your father do?  Business

He is a great player.      I don't think so.
 
She is my girlfriend.      I too agree.

我想在条件中从上述数据帧创建三个数据帧:

df1 的条件:

如果 df['Questions'] 的第一个单词来自列表:

# list of Yes/No verbs
yn_list = ['Do','Does','Did','do','does','did','Am','Are','Is','Was','Were','am','are','is','was','were',
           'Have','Has','Had','have','has','had','Will','Would','Shall','Should','Can','Could','May',
           'Might','will','would','shall','should','can','could','may','might']

# list of negative Yes/No verbs
yn_negative_list = ["Don't","Doesn't","Didn't","don't","doesn't","didn't","Aren't","Isn't","aren't","isn't",
                    "Wasn't","Weren't","wasn't","weren't","Haven't","Hasn't","Hadn't","haven't","hasn't",
                    "hadn't","Won't","Wouldn't","won't","wouldn't","Shan't","shan't","Shouldn't","Can't",
                    "Couldn't","shouldn't","can't","couldn't","may not","May not","Mightn't","mightn't"]

df2 的条件:

如果 df['Questions'] 的第一个单词来自列表:

wh_list = ['who','where','what','when','why','whom','which','whose','how']

df3 的条件:

如果句子以“.”结尾 标志

回答

你的第三个条件:

df[df['Question'].str.endswith('.')]

                Question                   Answer
4  He is a great player.        I don't think so.
5  She is my girlfriend.             I too agree.

条件二:

df[df['Question'].str.lower().str.startswith(tuple(wh_list))]

                    Question         Answer
0           Where is Amazon?         Brazil
3  What does your father do?       Business

第一个条件:

df[df['Question'].str.lower().str.startswith(tuple(yn_list+yn_negative_list))]

                 Question       Answer
1      Is he a scientist?           No
2  Did he stole my money?          Yes

  • TIL that `.str.startswith` accepts a tuple. But I can't find anything in [documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.startswith.html) +1

以上是如何根据指定的条件将数据帧划分为新的数据帧?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>