Список не переносится в Dataframe правильно - PullRequest
0 голосов
/ 20 апреля 2020

Я хочу проверить длину предложения в длинном столбце фрейма данных и вернуть другой фрейм данных len (предложение) * слово замены. Это образец предложений, длины которых я хочу проверить.

"But for his attorney's incompetence",
 'should there have been more supervision from the parents while their children were in the kitchen',
 "If I didn't have the option of makeup (concealer)",
 'if she had foregone insurance and printed off a savings coupon from the website GoodRx',
 'provided that his name were Rand Smith, or his father were Ron Paul the car salesman rather than Ron Paul the almost-libertarian presidential candidate',


for ant in range(len(antecedents)):
    replace_tag = 'ante'   #the replacement word
    ant_to_string = ' '.join([str(elem) for elem in antecedents])  #convert to string
    get_words = ant_to_string.split(" ")  #split string
    phrase_tag.append(list((replace_tag,) * len(get_words)))#multiply string for each word in the instance
df = pd.DataFrame(phrase_tag, columns=['labels'])#fill in dataframe

и вместо фрейма данных из 3550 строк я получаю фрейм данных из 49000i sh строк

bound method NDFrame.sample of                                                   labels
0                                                   ante
1                                                   ante
2                                                   ante
3                                                   ante
4                                                   ante
...                                                  ...
49583  [ante, ante, ante, ante, ante, ante, ante, ant...
49584  [ante, ante, ante, ante, ante, ante, ante, ant...
49585  [ante, ante, ante, ante, ante, ante, ante, ant...
49586  [ante, ante, ante, ante, ante, ante, ante, ant...
49587  [ante, ante, ante, ante, ante, ante, ante, ant...

что я делаю не так?

1 Ответ

1 голос
/ 20 апреля 2020

Если предположить, что antecedents является столбцом в кадре данных, то вы должны сделать следующее:

replace_tag = 'ante'
newcol = antecedents.apply(lambda x: [replace_tag] * len(x.split()))

Пример

df = pd.DataFrame({'antecedents': ['I love ice cream', 'I hate ice cream more']})
replace_tag = 'ante'
df['antecedents'].apply(lambda x: [replace_tag] * len(x.split()))

=== Output: ===

0          [ante, ante, ante, ante]
1    [ante, ante, ante, ante, ante]
Name: antecedents, dtype: object
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...