简体   繁体   English

将带有数字列的Pandas数据框另存为Excel中的文本

[英]Save Pandas dataframe with numeric column as text in Excel

I am trying to export a Pandas dataframe to Excel where all columns are of text format. 我正在尝试将Pandas数据框导出到Excel,其中所有列均为文本格式。 By default, the pandas.to_excel() function lets Excel decide the data type. 默认情况下,pandas.to_excel()函数使Excel可以确定数据类型。 Exporting a column with [1,2,'w'] results in the cells containing 1 and 2 to be numeric, and the cell containing 'w' to be text. 导出带有[1,2,'w']的列会导致包含1和2的单元格为数字,而包含'w'的单元格为文本。 I'd like all rows in the column to be text (ie ['1','2','w']). 我希望该列中的所有行均为文本(即['1','2','w'])。

I was able to solve the problem by assigning the column I need to be text using the .astype(str). 我可以通过使用.astype(str)分配需要为文本的列来解决该问题。 However, if the data is large, I am concerned that I will run into performance issues. 但是,如果数据很大,我担心会遇到性能问题。 If I understand correctly, df[col] = df[col].astype(str) makes a copy of the data, which is not efficient. 如果我理解正确,则df [col] = df [col] .astype(str)会复制数据,效率不高。

import pandas as pd

df = pd.DataFrame({'a':[1,2,'w'], 'b':['x','y','z']})

df['a'] = df['a'].astype(str)

df.to_excel(r'c:\tmp\test.xlsx')

Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点?

I searched SO several times and didn't see anything on this. 我搜索了几次,但没有看到任何内容。 Forgive me if this has been answered before. 如果以前已经回答过,请原谅我。 This is my first post, and I'm really happy to participate in this cool forum. 这是我的第一篇文章,我很高兴参加这个很棒的论坛。

Edit: Thanks to the comments I've received, I see that Converting a series of ints to strings - Why is apply much faster than astype? 编辑:感谢收到的评论,我看到将一系列int转换为字符串-为什么应用比astype快得多? gives me other options to astype(str). 给我astype(str)的其他选项。 This is really useful. 这真的很有用。 I also wanted to know if astype(str) was inefficient because it made a copy of the data, which I now see that it does not. 我还想知道astype(str)是否效率低下,因为它制作了数据的副本,现在我发现它不是。

I don't think that you'll not have performance issues with that approach since data is not copied but replaced. 我认为这种方法不会带来性能问题,因为数据不是复制而是替换的。 You may also convert the whole dataframe into string type using 您也可以使用以下命令将整个dataframe转换为string类型

df = df.astype(str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM