Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-Pandas-导入Excel文件,遍历每一行,添加新值,并添加到dataframe_Python_Excel_Pandas_Loops_Import From Excel - Fatal编程技术网

Python-Pandas-导入Excel文件,遍历每一行,添加新值,并添加到dataframe

Python-Pandas-导入Excel文件,遍历每一行,添加新值,并添加到dataframe,python,excel,pandas,loops,import-from-excel,Python,Excel,Pandas,Loops,Import From Excel,我有一个Excel文件,其中有一个项目代码和需要导入的摘要字段,这样我就可以在摘要上运行一个简单的文本摘要器,然后添加到数据框中 我的Excel数据集如下所示: [Proj_Number] | [Abstract] JJF-123 | Diabetes is a serious chronic condition. JFR-223 | Cardiovascular disease is also a chronic condition. JF3-334

我有一个Excel文件,其中有一个项目代码和需要导入的摘要字段,这样我就可以在摘要上运行一个简单的文本摘要器,然后添加到数据框中

我的Excel数据集如下所示:

[Proj_Number] | [Abstract]

JJF-123          | Diabetes is a serious chronic condition.  
JFR-223          | Cardiovascular disease is also a chronic condition. 
JF3-334          | Don't forget about asthma and how much it sucks. 
导入数据后,我希望应用我的文本摘要器并获得以下结果:

[Proj_Number] | [Abstract]                        [Ab_keywords]

JJF-123       | Diabetes is a chronic condition.  |Diabetes, chronic condition                                                                 
JFR-223       | COPD is a also chronic condition. | COPD, chronic condition
JF3-334       | Don't forget about asthma too.    | asthma, forgot
我知道我的代码是错误的,但我只是不知道如何循环每一行,从摘要中获取抽象关键字,将其添加到数据帧,然后导出它

从gensim.summary.summarier导入摘要
从gensim.summation导入关键字
作为pd进口熊猫
dataset=pd.read\u excel('abstracts.xlsx',encoding=“ISO-8859-1”)
df=pd.DataFrame(数据集)
cols=[1,2]
df=df[df.columns[cols]]
对于df中的d:
d=关键词(d,比率=0.15,分割=True))
印刷品(d)

您不想用df中d的
迭代df中的每一行:

Pandas可以将函数应用于数据帧的每一行,并通过
apply
函数返回序列

如果您适当地重命名数据框的列

df['Ab_keywords']=df['Abstract'].apply(lambda text:keywords(text,ratio=0.15,split=True))

应该有用


这里lambda函数应用于df['Abstract']的每一行,并给出每一行的值作为其参数。

感谢您的回复。我已经用您添加的代码替换了df:section中的for d,得到了“keyrerror:‘Abstract’”——有什么想法吗?我修改了上面的代码。提前谢谢。事实上,呃,我打错专栏了。你的代码成功了,谢谢!太好了,你能不能把原来的问题保持原样,以防其他人遇到同样的问题?