Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading date from a data frame based on conditions in a different data frame

Tags:

python

pandas

I have 2 data frames. I need to read in values from one data frame based on values from another

words:

words = pd.DataFrame()
words['no'] = [1,2,3,4,5,6,7,8,9]
words['word'] = ['cat', 'in', 'hat', 'the', 'dog', 'in', 'love', '!', '<3']
words

Sentences:

sentences =  pd.DataFrame()
sentences['no'] =[1,2,3]
sentences['start'] = [1, 4, 6]
sentences['stop'] = [3, 5, 9]
sentences

the desired output is in to a text file:

cat in hat
***
the dog
***
in love ! <3

however i cant get past this step, i have tried running the following code:

for x in sentances: print(words['word'][words['no'].between(sentences['start'], sentences['stop'], inclusive = True)

but am returned with this error

 File "<ipython-input-16-ae3f5333be66>", line 3
    print(words['word'][words['no'].between(sentences['start'], sentences['stop'], inclusive = True)
                                                                                                    ^
SyntaxError: unexpected EOF while parsing
like image 904
Pythonuser Avatar asked Dec 01 '25 06:12

Pythonuser


1 Answers

Set no as the index for words and then iterate over sentences using a list comprehension:

v = words.set_index('no')['word']
sentences = [
    ' '.join(v.loc[i:j]) for i, j in zip(sentences['start'], sentences['stop'])
]

Or index agnostic:

v = words['word'].tolist()
sentences = [
    ' '.join(v[i - 1:j - 1] for i, j in zip(sentences['start'], sentences['stop'])
]

['cat in hat', 'the dog', 'in love ! <3']

Saving to a file should be straightforward from here:

with open('file.txt', 'w') as f:
    for sent in sentences:
        f.write(sent + '\n')
        f.write('***\n')
like image 151
cs95 Avatar answered Dec 03 '25 20:12

cs95



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!