I am trying to find words with letters in specific positions within my dataframe. My dataframe is a list of all 5 letter words in English in all lower-case and no special characters (i.e. only alpha characters).
df = list of 5 letter words
word = column of words
Code:
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']
This will find all words that contain user inputed letters in the order inputed. While useful, it's not exactly what I'd like to do. How would I search for words that only contain letters in specific positions and print that list. For example:
First letter = t
Second Letter = r
Third Letter =
Fourth Letter = i
Fifth letter = n
Out: Train
I'm quite new to both python and jupyter and I would like to thank you for your help in advance.
CodePudding user response:
The most reasonable to me seems to use a fullmatch
regex:
# replace the character you inputed for unknown with "."
# Assuming space here
l1 = firstLetter.replace(' ', '.')
l2 = secondLetter.replace(' ', '.')
# same for all letters...
m = df['word'].fullmatch(f'{l1}{l2}{l3}{l4}{l5}')
df.loc[m]
Ideally you could even input the regex directly:
regex = input('enter the pattern with "." for unknown letters: ')
# example tr.in
m = df['word'].fullmatch(regex)
df.loc[m]
CodePudding user response:
Here is necessary return Trues if no value is typing in input (empty string), so mask for test values by positions is:
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']
Or is possible create more general solution from above:
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']
Test:
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
CodePudding user response:
L1=['magic','sweet','nails','squiz']
df=pd.DataFrame({'words':L1})
##Create a column for each of the letter .
df['first']=df['words'].str[0]
df['second']=df['words'].str[1]
df['third']=df['words'].str[2]
df['fourth']=df['words'].str[3]
df['fifth']=df['words'].str[4]
display(df)
words first second third fourth fifth
0 magic m a g i c
1 sweet s w e e t
2 nails n a i l s
3 squiz s q u i z
## Now you can filter this dataframe using query
# example all words with 's' in first place :
df.query('first=="s"')
## you can add multiple filters
df.query('first=="s" and second=="t"')
CodePudding user response:
Try this, its easier to parse the data with list.
import re
wlist = ['crate', 'fight', 'aroma']
char = ['*','*','*','*','*']
char[0] = input('1 : ')
char[1] = input('2 : ')
char[2] = input('3 : ')
char[3] = input('4 : ')
char[4] = input('5 : ')
regex = ''.join([i if i !='' else r'\w' for i in char])
print([w for w in wlist if re.search(regex ,w)])