如何将数据类型的 pandas 数据框更改为定义格式的字符串? [英] How do I change data-type of pandas data frame to string with a defined format?
问题描述
print image_name_data
id image_name
0 1001 1001_mar2014_report
1 1002 1002_mar2014_report
2 1003 1003_mar2014_report
[3 rows x 2 columns]
print image_name_data.dtypes
id float64
image_name object
dtype:object
问题在于,id列中的数字实际上是身份证号码,我需要把它们作为字符串。我已经尝试将ID列转换为字符串使用:
$ p $ image_name_data ['id'] = image_name_data ['id']。 astype('str')
这看起来有点难看,但确实产生了一个类型为object '而不是'float64':
print image_name_data.dyptes
id对象
image_name对象
dtype:object
但是,创建的字符串有一个小数点,如下所示: p>
print image_name_data
id image_name
0 1001.0 1001_mar2014_report
1 1002.0 1002_mar2014_report
2 1003.0 1003_mar2014_report
[3行x 2列]
如何转换在一个pandas DataFrame中的float64列转换为一个给定格式的字符串(在这种情况下,例如'%10.0f')? 我无法重现你的问题但是你有尝试将它转换为一个整数第一?
image_name_data ['id'] = image_name_data ['id']。astype(int ).astype('str')
然后,关于更一般的问题,可以使用 map
(在这个答案)。在你的情况:
image_name_data ['id'] = image_name_data ['id']。map('{:。0f}'。format)
I'm starting to tear my hair out with this - so I hope someone can help. I have a pandas DataFrame that was created from an Excel spreadsheet using openpyxl. The resulting DataFrame looks like:
print image_name_data
id image_name
0 1001 1001_mar2014_report
1 1002 1002_mar2014_report
2 1003 1003_mar2014_report
[3 rows x 2 columns]
…with the following datatypes:
print image_name_data.dtypes
id float64
image_name object
dtype: object
The issue is that the numbers in the id column are, in fact, identification numbers and I need to treat them as strings. I've tried converting the id column to strings using:
image_name_data['id'] = image_name_data['id'].astype('str')
This seems a bit ugly but it does produce a variable of type 'object' rather than 'float64':
print image_name_data.dyptes
id object
image_name object
dtype: object
However, the strings that are created have a decimal point, as shown:
print image_name_data
id image_name
0 1001.0 1001_mar2014_report
1 1002.0 1002_mar2014_report
2 1003.0 1003_mar2014_report
[3 rows x 2 columns]
How can I convert a float64 column in a pandas DataFrame to a string with a given format (in this case, for example, '%10.0f')?
I'm unable to reproduce your problem but have you tried converting it to an integer first?
image_name_data['id'] = image_name_data['id'].astype(int).astype('str')
Then, regarding your more general question you could use map
(as in this answer). In your case:
image_name_data['id'] = image_name_data['id'].map('{:.0f}'.format)
这篇关于如何将数据类型的 pandas 数据框更改为定义格式的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!