gpt4 book ai didi

python - 如何在数据框中的特定位置查找带有字母的单词 - Jupyter

转载 作者:行者123 更新时间:2023-12-01 23:04:46 25 4
gpt4 key购买 nike

我正在尝试在我的数据框中的特定位置查找带有字母的单词。我的数据框是所有 5 个英文字母单词的列表,全部小写且没有特殊字符(即只有字母字符)。

df = 5 个字母的单词列表

word = 单词列

代码:

firstLetter = input('First Letter = ')
secondLetter = input('Second Letter = ')
thirdLetter = input('Third Letter = ')
fourthLetter = input('Fourth Letter = ')
fifthLetter = input('Fifth Letter = ')
total = str(firstLetter)+str(secondLetter)+str(thirdLetter)+str(fourthLetter)+str(fifthLetter)
df[df['word'].str.contains(total)]['word']

这将按输入顺序查找包含用户输入字母的所有单词。虽然有用,但这并不是我想要做的。我将如何搜索仅包含特定位置字母的单词并打印该列表。例如:

First letter = t
Second Letter = r
Third Letter =
Fourth Letter = i
Fifth letter = n

Out: Train

我对 python 和 jupyter 都很陌生,在此先感谢您的帮助。

最佳答案

如果没有输入值(空字符串),这里有必要返回Trues,因此按位置测试值的掩码是:

firstLetter = input('First Letter = ')
secondLetter = input('Second Letter = ')
thirdLetter = input('Third Letter = ')
fourthLetter = input('Fourth Letter = ')
fifthLetter = input('Fifth Letter = ')

m1 = df['word'].str[0].eq(firstLetter) | (not bool(firstLetter))
m2 = df['word'].str[1].eq(secondLetter) | (not bool(secondLetter))
m3 = df['word'].str[2].eq(thirdLetter) | (not bool(thirdLetter))
m4 = df['word'].str[3].eq(fourthLetter) | (not bool(fourthLetter))
m5 = df['word'].str[4].eq(fifthLetter) | (not bool(fifthLetter))

s = df.loc[m1 & m2 & m3 & m4 & m5, 'word']

或者可以从上面创建更通用的解决方案:

firstLetter = input('First Letter = ')
secondLetter = input('Second Letter = ')
thirdLetter = input('Third Letter = ')
fourthLetter = input('Fourth Letter = ')
fifthLetter = input('Fifth Letter = ')

tup = (firstLetter, secondLetter, thirdLetter, fourthLetter, fifthLetter)
m = [df['word'].str[i].eq(v) | (not bool(v)) for i, v in enumerate(tup)]

s = df.loc[np.logical_and.reduce(m), 'word']

测试:

print (df)
word
0 train
1 yrasn

firstLetter = input('First Letter = ')
secondLetter = input('Second Letter = ')
thirdLetter = input('Third Letter = ')
fourthLetter = input('Fourth Letter = ')
fifthLetter = input('Fifth Letter = ')

First Letter = t

Second Letter = r

Third Letter =

Fourth Letter = i

Fifth Letter = n

tup = (firstLetter, secondLetter, thirdLetter, fourthLetter, fifthLetter)
print (tup)
('t', 'r', '', 'i', 'n')

m = [df['word'].str[i].eq(v) | (not bool(v)) for i, v in enumerate(tup)]

s = df.loc[np.logical_and.reduce(m), 'word']
print (s)
0 train
Name: word, dtype: object

关于python - 如何在数据框中的特定位置查找带有字母的单词 - Jupyter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71153198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com