假设我有这个数据
import pandas as pd
df = pd.DataFrame({'Verbatim': ['Pants', 'Shirts', 'Shirts', 'Pants', 'Shoes', 'Shoes', 'Shoes', 'Shoes', 'Dresses, Shoes, Dresses', 'Pants', 'Pants', 'Shirts', 'Dresses Pants Shoes', 'Shoes Pants', 'Pants', 'Pants', 'Dresses', 'Pants', 'Pants', 'Dresses']})通过不同的步骤,我确定了我在上面所有独特的单词是
unique_words = ('Pants', 'Shirts', 'Shoes', 'Dresses')现在,我想在我的数据框架中添加列,表示“逐字”列中每个唯一单词的存在。我是从逐字文本创建假人。因此,如果被调查者在回答中注意到“着装”,他们会在着装栏中得到1分。
如何使用循环/apply语句来实现自动化?我想做这样的事
for word in unique_words:
df['word'] = 0
df.loc[df['Verbatim'].str.contains("word"), 'word'] = 1本质上,我想知道如何使用迭代器('word')在一个以与该迭代器相同的名称命名的dataframe中创建列。如何引用循环中的迭代器?这段代码是手动工作的,但我不知道如何循环它。
谢谢!
发布于 2022-08-18 00:50:20
您可以使用apply函数:
for word in unique_words:
df[word] = df.apply(lambda x: word in x["Verbatim"], axis=1)
print(df.head()产出如下:
Verbatim Pants Shirts Shoes Dresses
0 Pants True False False False
1 Shirts False True False False
2 Shirts False True False False
3 Pants True False False False
4 Shoes False False True False如果你不喜欢这样的类型,你可以把0和1放在下面:
for word in unique_words:
df[word] = df.apply(lambda x: 1 if word in x["Verbatim"] else 0, axis=1)
print(df.head())其结果是:
Verbatim Pants Shirts Shoes Dresses
0 Pants 1 0 0 0
1 Shirts 0 1 0 0
2 Shirts 0 1 0 0
3 Pants 1 0 0 0
4 Shoes 0 0 1 0https://stackoverflow.com/questions/73396289
复制相似问题