сбор информации формирует множество кадров данных на основе кадра данных эталонного - PullRequest
0 голосов
/ 01 июня 2018

Здравствуйте, я пытаюсь составить предложения, слова взяты из первых 3 фреймов данных

df1=pd.DataFrame()
    df1['w']=['i', 'am', 'python', 'is', 'set', 'sail']
    df1['n'] = [1,2,3,4,5,6]
    df2 =pd.DataFrame()
    df2['w']=['i', 'wish', 'in', 'love', 'has' ]
    df2['n'] =[1,2,3,4,5]
    df3 = pd.DataFrame()
    df3['w']=['the', 'ship', 'with', 'you', 'my', 'friend']
    df3['n']=[1,2,3,4,5,6]

здесь определены части и места для слов каждого предложения и границы

string= pd.DataFrame()
string['location'] = ['df1', 'df2', 'df3', 'df2', 'df1', 'df3', 'df3', 'df2', 'df1']
string['start'] = [1, 3, 3, 1, 3, 5, 1, 5, 5]
string['stop'] = [2 , 4, 4, 1, 4, 6, 2, 5, 6]
string['sentence] = [1,1,1,2,2,2,3,3,3]
string['part'] = [1, 1, 1, 1, 1, 1, 2, 2, 2]

желаемый результат:

i am in love with you 
i wish pyhton is my fried
**boundry**
the ship has set sail
**boundry**

код, который я пробовал, я нашел, что он далеко, кажется, делает большую часть того, что я хочу, но я сократил понять, как Mae работает с несколькимитаблицы, и получить заказ, который я после.

x = df1.set_index('n')['w']
sent = [
    ' '.join(x.loc[i:j]) for i, j in zip(string['start'], string['stop'])
]

sent

вывод я получаю

['i am',
 'python is',
 'python is',
 'i',
 'python is',
 'set sail',
 'i am',
 'set',
 'set sail']

1 Ответ

0 голосов
/ 01 июня 2018

попробуйте это,

string['res'] = string.apply(lambda x: ' '.join(globals()[x['location']].iloc[(x['start']-1):(x['stop'])]['w']),axis=1)
print string['res'].values.tolist()

вывод:

['i am', 'in love', 'with you', 'i wish', 'python is', 'my friend', 'the ship', 'has', 'set sail']

Для дальнейшего результата (Добавление границы):

string['res'] = string.apply(lambda x: ' '.join(globals()[x['location']].iloc[(x['start']-1):(x['stop'])]['w']),axis=1)
string.loc[~string['part'].duplicated(keep='last'),'flag']='boundry'

l=list(string['res'].values)
b = list(np.where(string['flag'].values == 'boundry')[0])
[l.insert(ind+i,'boundary') for i,ind in enumerate(b,1)]
print l

Вывод:

['i am', 'in love', 'with you', 'i wish', 'python is', 'my friend', 'boundary', 'the ship', 'has', 'set sail', 'boundary']
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...