【发布时间】:2021-04-26 14:51:41
【问题描述】:
所以我有一个列表和一个数据框。我想从列表中取出单词并将其作为列的标题。如果这个词是它添加到新创建的列的行。如果它不在行中,请留空或不适用。 我应该使用 iloc 吗?
import pandas as pd
wordlist = [['this is sentence 1'],['this is sentence 2'],['this is not a sentence'],['ok who is this']]
query=['is','not']
df = pd.DataFrame(wordlist, columns = ['Name'])
for word in query:
if word in df['Name']:
df[word] = word
df
Output
Name is not <<column titles
0 this is sentence 1 is NA
1 this is sentence 2 is NA
2 this is not a sentence is not
3 ok who is this is NA
【问题讨论】:
标签: python-3.x pandas dataframe if-statement