向数据框添加值并导出

Adding values to Data frame and Export

我正在尝试将两个值作为列表添加到数据框中 一个是句子,另一个是我得到的单词列表,在对这些句子进行标记后

目前,我已经完成了以下代码

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) 

预期输出为

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

例子

原句 |代币

我叫max |我的名字是最大

这是windows |这,就是,windows

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 直接输出到您的数据库,需要针对您的数据库进行特定设置。参考这里例如: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html