首页 > 解决方案 > 使用 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')

似乎不起作用。

标签: pythonpandas

解决方案


尝试这个

no_mails= pd.DataFrame({'email' : []}) no_mails['email'] = emails(data) no_mails.to_excel('new_data.xlsx')


推荐阅读