将值添加到数据框和导出

问题描述 投票:2回答:1

我试图在数据框中添加两个值作为列表,一个是句子,另一个是我得到的单词列表,在这些句子标记化之后

现在,我已经完成了以下代码

from nltk.tokenize import word_tokenize
example = ['Mary had a little lamb' , 
        'Jack went up the hill' , 
        'Jill followed suit' ,    
        'i woke up suddenly' ,
       'it was a really bad dream...']


def hi():
    for i in example:
        #print (word_tokenize(i),i)
        a=[i,word_tokenize(i)]

        print(a) 

预期的产出是

数据框有两列,原始句子和该句子的标记

Orignal Sentence |令牌

我的名字是max |我的姓名,就是最大

这是windows |这是,windows

python dataframe tokenize
1个回答
1
投票
df['Original Sentence'] = a[0]  
df['Tokens'] = a[1]

或者我们可以完全跳过你的功能:

df['Original Sentence'] = example
df['Tokens'] = [word_tokenize(i) for i in example]

编辑: 因为看起来你没有开始的数据帧。

import pandas as pd
df = pd.DataFrame.from_dict({'Original Sentence': example,
                   'Tokens': [word_tokenize(i) for i in example]})
print(df) #to see your dataframe 
df.to_csv('mydata.csv') #To output your dataframe into a csv file  

其他格式:

df.to_sql(etc...) #Refer to comment below  

要作为sql direct直接输出到数据库,需要特定于db的设置。请参考此处:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html

© www.soinside.com 2019 - 2024. All rights reserved.