python 用pandas将所有单元格格式转换成文本
要将所有单元格格式转换为文本,可以使用pandas库中的astype()方法。以下是示例代码:
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [1, 2, 3],
'B': [4.5, 5.6, 6.7],
'C': ['7', '8', '9']}
df = pd.DataFrame(data)
# 将所有单元格格式转换为文本
df = df.astype(str)
print(df.dtypes)
print(df)
输出结果如下:
A object
B object
C object
dtype: object
A B C
0 1 4.5 7
1 2 5.6 8
2 3 6.7 9
在上述示例中,首先创建了一个示例DataFrame,然后使用astype()方法将所有单元格格式转换为文本。最后,分别打印了DataFrame的数据类型和内容。
原文地址: https://www.cveoy.top/t/topic/ibQY 著作权归作者所有。请勿转载和采集!