使用Pandas导出数据

2024-05-12 19:11:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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>

我的脚本:

^{pr2}$

我的输出:

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')

似乎不起作用。在


Tags: tonodataenglishisemailmyphone
3条回答

您可以在熊猫系列中使用replace

df['C'] = df['C'].str.replace(r'[\w\.-]+@[\w\.-]+','<adresse_mail>')
df.to_excel('new_data.xlsx')

试试这个

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

to_excel是熊猫数据帧方法doc。您应该对数据帧执行替换,而不是对提取为字符串的列执行替换(就像您对:Series.to_string(df['C'])所做的那样)。在

坚持数据帧,你应该是好的。在

相关问题 更多 >