简体   繁体   English

如何将所有字符串(如“故障”)转换为唯一的浮点数?

[英]How do I convert all strings (like “Fault”) and into a unique float?

I have a DataFrame that has int , float , and object (strings with characters) items in it. 我有一个DataFrame,其中包含intfloatobject (带字符的字符串)项。 I want a unique float for every unique object like so- 我希望每个独特的物体都有一个独特的浮子,如此

Exhuast
Fault
Probation
Exhaust
Fault
Motor

to

1.
2.
3.
1.
2.
4.

Also, will it work on all of the columns or would I have to do column by column? 此外,它是否适用于所有列,还是我必须逐列?

Last question, will it also convert all the int 's to float 's as well? 最后一个问题,它还将所有的int转换为float吗?

As mentioned by Jon, you could make use of Series.factorize . 正如Jon所说,你可以使用Series.factorize

(s.factorize()[0]+1).astype('float')

To perform this column-wise over an entire DataFrame, just use apply . 要在整个DataFrame上按列执行此操作,只需使用apply

Demo 演示

>>> s = pd.Series(['Exhaust', 'Fault', 'Probation', 5, int,
                   'Exhaust', int, 'Fault', 'Motor'])

>>> s
0          Exhaust
1            Fault
2        Probation
3                5
4    <class 'int'>
5          Exhaust
6    <class 'int'>
7            Fault
8            Motor
dtype: object

>>> (s.factorize()[0]+1).astype('float')
array([ 1.,  2.,  3.,  4.,  5.,  1.,  5.,  2.,  6.])

A NumPy solution may be to use the return_inverse keyword arg of np.unique , 甲NumPy的解决方案可以是使用return_inverse的关键字ARG np.unique

(np.unique(s, return_inverse=True)[1]+1).astype('float')

however from some rough benchmarking the Pandas solution may be faster. 然而,从一些粗略的基准测试来看,Pandas解决方案可能会更快。

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

相关问题 在pandas中,如何将一系列float或none转换为带整数的字符串 - In pandas, how do I convert a series of float or none to strings with integers 如何在 Python 中将货币字符串列表转换为浮点数? - How do I convert a list of currency strings into float in Python? 如何修改字符串的python数组以去除所有字母数字,转换为float,然后将float存储在同一数组中? - How do I modify a python array of strings to strip any alphanumerics, convert to float, and then store the float in the same array? 如何将嵌套列表转换为浮点数? - How do I convert a nested list into a float? 如何将字符串列表转换为浮点数数组? - How can I convert a list of strings to an array of float numbers? 在数据框中替换 &#39;%&#39;,然后将所有字符串转换为浮点数 - Replace in dataframe the '%' and then convert all the strings to float 如何将浮点十进制转换为浮点八进制/二进制? - How do I convert float decimal to float octal/binary? 如何将浮点字符串转换为整数字符串? - How to convert float strings to integer strings? 数据框列的数字很多是 str 格式,很多是浮点数,我如何将它们全部转换为浮点数 - Dataframe columns have numbers many of them in str format and many in float, how do I convert all of them to float 如何替换所有字符串'? 用 0 浮点数? - How to replace all strings '?' with a 0 float?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM