Python 在 Pandas 中将浮点数转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23158447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:21:31  来源:igfitidea点击:

Convert Float to String in Pandas

pythonpandas

提问by Rohit

I am a little confused with datatype "object" in Pandas. What exactly is "object"?

我对 Pandas 中的数据类型“对象”有点困惑。“对象”究竟是什么?

I would like to change the variable "SpT" (see below) from object to String.

我想将变量“SpT”(见下文)从对象更改为字符串。

> df_cleaned.dtypes
    Vmag        float64
    RA          float64
    DE          float64
    Plx         float64
    pmRA        float64
    pmDE        float64
    B-V         float64
    SpT          object
    M_V         float64
    distance    float64
    dtype: object

For this I do the following:

为此,我执行以下操作:

df_cleaned['SpT'] = df_cleaned['SpT'].astype(str)

But that has no effect on the dtype of SpT.

但这对 SpT 的 dtype 没有影响。

The reason for doing is when I do the following:

这样做的原因是当我执行以下操作时:

f = lambda s: (len(s) >= 2)  and (s[0].isalpha()) and (s[1].isdigit())
i  = df_cleaned['SpT'].apply(f)
df_cleaned = df_cleaned[i]

I get:

我得到:

TypeError: object of type 'float' has no len()

Hence, I believe if I convert "object" to "String", I will get to do what I want.

因此,我相信如果我将“对象”转换为“字符串”,我就会做我想做的事。

More info: This is how SpT looks like:

更多信息:这就是 SpT 的样子:

HIP
1                F5
2               K3V
3                B9
4               F0V
5             G8III
6              M0V:
7                G0
8      M6e-M8.5e Tc
9                G5
10              F6V
11               A2
12            K4III
13            K0III
14               K0
15               K2
...
118307    M2III:
118308        K:
118309        A2
118310        K5
118312        G5
118313        F0
118314        K0
118315     K0III
118316        F2
118317        F8
118318        K2
118319       G2V
118320        K0
118321       G5V
118322      B9IV
Name: SpT, Length: 114472, dtype: object

采纳答案by YS-L

If a column contains string or is treated as string, it will have a dtypeof object(but not necessarily true backward -- more below). Here is a simple example:

如果一列包含字符串或被视为字符串,则它将有一个dtypeof object(但不一定是向后的 - 更多信息)。这是一个简单的例子:

import pandas as pd
df = pd.DataFrame({'SpT': ['string1', 'string2', 'string3'],
                   'num': ['0.1', '0.2', '0.3'],
                   'strange': ['0.1', '0.2', 0.3]})
print df.dtypes
#SpT        object
#num        object
#strange    object
#dtype: object

If a column contains only strings, we can apply lenon it like what you did should work fine:

如果一列只包含字符串,我们可以len像你所做的那样应用它,应该可以正常工作:

print df['num'].apply(lambda x: len(x))
#0    3
#1    3
#2    3

However, a dtypeof object does not means it only contains strings. For example, the column strangecontains objects with mixed types -- and some strand a float. Applying the function lenwill raise an error similar to what you have seen:

但是,dtype对象的a并不意味着它只包含字符串。例如,该列strange包含具有混合类型的对象 - 以及 somestr和 a float。应用该函数len将引发类似于您所看到的错误:

print df['strange'].apply(lambda x: len(x))
# TypeError: object of type 'float' has no len()

Thus, the problem could be that you have not properly converted the column to string, and the column still contains mixed object types.

因此,问题可能是您没有正确地将列转换为字符串,并且该列仍然包含混合对象类型。

Continuing the above example, let us convert strangeto strings and check if applyworks:

继续上面的例子,让我们转换strange为字符串并检查是否apply有效:

df['strange'] = df['strange'].astype(str)
print df['strange'].apply(lambda x: len(x))
#0    3
#1    3
#2    3

(There is a suspicious discrepancy between df_cleanedand df_cleanthere in your question, is it a typo or a mistake in the code that causes the problem?)

(在您的问题中df_cleaneddf_clean那里之间存在可疑的差异,是代码中的错字还是错误导致了问题?)