python - 使用 pandas 导出数据
问题描述
我在 excel 文件的列中应用了处理。现在,我想导出这个处理过的列以及所有其他未处理的列。
我的数据(小例子):
A B C
French house Phone <phone_numbers>
English house email blablabla@gmail.com
French apartment my name is Liam
French house Hello George
English apartment Ethan, my phone is <phone_numbers>
我的脚本:
import re
import pandas as pd
from pandas import Series
df = pd.read_excel('data.xlsx')
data = Series.to_string(df['C'])
def emails(data):
mails = re.compile(r'[\w\.-]+@[\w\.-]+')
replace_mails = mails.sub('<adresse_mail>', data)
return replace_mails
no_mails = emails(data)
no_mails.to_excel('new_data.xlsx')
我的输出:
AttributeError Traceback (most recent call last)
<ipython-input-7-8fd973998937> in <module>()
7
8 no_mails = emails(data)
----> 9 no_mails.to_excel('new_data.xlsx')
AttributeError: 'str' object has no attribute 'to_excel'
良好的输出:
A B C
French house Phone <phone_numbers>
English house email <adresse_mail>
French apartment my name is Liam
French house Hello George
English apartment Ethan, my phone is <phone_numbers>
我的脚本工作正常,只有
no_mails.to_excel('new_data.xlsx')
似乎不起作用。
解决方案
尝试这个
no_mails= pd.DataFrame({'email' : []})
no_mails['email'] = emails(data)
no_mails.to_excel('new_data.xlsx')
推荐阅读
- php - 只允许发布某些输入
- ionic-framework - 带有 Ionic v5 (react) 的 React-testing-library 和 react-hook-form-change 事件不会触发
- python - 无法在 Windows 10 上安装 PyGTK
- php - 保留 PHP 对象数组中的某些键
- flutter - 如何在本地身份验证中单击取消以及当最大尝试次数超过颤振时关闭应用程序
- sql-server-data-tools - SSDT - 预构建自动化 - 为存储过程设置“buildAction”标志
- php - foreach cicle 与前一行表的数学
- java - IllegalStateException LogicalConnectionManagedImpl is closed Hibernate
- javascript - 显示电子邮件的自定义有效性消息?
- reactjs - 单击提交按钮后使用反应清除表单中的受控组件