Python 熊猫将浮点数转换为没有小数的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38516316/
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
pandas converting floats to strings without decimals
提问by Brian
I have a dataframe
我有一个数据框
df = pd.DataFrame([
['2', '3', 'nan'],
['0', '1', '4'],
['5', 'nan', '7']
])
print df
0 1 2
0 2 3 nan
1 0 1 4
2 5 nan 7
I want to convert these strings to numbers and sum the columns and convert back to strings.
我想将这些字符串转换为数字并对列求和并转换回字符串。
Using astype(float)
seems to get me to the number part. Then summing is easy with sum()
. Then back to strings should be easy too with astype(str)
使用astype(float)
似乎让我进入了数字部分。然后用 很容易求和sum()
。然后回到字符串也应该很容易astype(str)
df.astype(float).sum().astype(str)
0 7.0
1 4.0
2 11.0
dtype: object
That's almost what I wanted. I wanted the string version of integers. But floats have decimals. How do I get rid of them?
这几乎就是我想要的。我想要整数的字符串版本。但是浮点数有小数。我该如何摆脱它们?
I want this
我要这个
0 7
1 4
2 11
dtype: object
采纳答案by piRSquared
Add a astype(int)
in the mix:
astype(int)
在混合物中添加一个:
df.astype(float).sum().astype(int).astype(str)
0 7
1 4
2 11
dtype: object
Demonstration of example with empty cells. This was not a requirement from the OP but to satisfy the detractors
演示带有空单元格的示例。这不是 OP 的要求,而是为了满足批评者的要求
df = pd.DataFrame([
['2', '3', 'nan', None],
[None, None, None, None],
['0', '1', '4', None],
['5', 'nan', '7', None]
])
df
0 1 2 3
0 2 3 nan None
1 None None None None
2 0 1 4 None
3 5 nan 7 None
Then
然后
df.astype(float).sum().astype(int).astype(str)
0 7
1 4
2 11
3 0
dtype: object
Because the OP didn't specify what they'd like to happen when a column was all missing, presenting zero is a reasonable option.
因为 OP 没有指定当一列全部丢失时他们希望发生什么,所以呈现零是一个合理的选择。
However, we could also drop those columns
但是,我们也可以删除这些列
df.dropna(1, 'all').astype(float).sum().astype(int).astype(str)
0 7
1 4
2 11
dtype: object
回答by maxymoo
Converting to int
(i.e. with .astype(int).astype(str)
) won't work if your column contains nulls; it's often a better idea to use string formatting to explicitly specify the format of your string column; (you can set this in pd.options
):
如果您的列包含空值,则转换为int
(即 with .astype(int).astype(str)
)将不起作用;使用字符串格式来明确指定字符串列的格式通常是一个更好的主意;(您可以在 中设置pd.options
):
>>> pd.options.display.float_format = '{:,.0f}'.format
>>> df.astype(float).sum()
0 7
1 4
2 11
dtype: float64
回答by Jossie Calderon
Add astype(int)
right before conversion to a string:
astype(int)
在转换为字符串之前添加:
print (df.astype(float).sum().astype(int).astype(str))
Generates the desired result.
生成所需的结果。
回答by toto_tico
For pandas >= 1.0:
对于 >= 1.0 的熊猫:
<NA>
type was introduced for 'Int64'. You can now do this:
<NA>
类型是为 'Int64' 引入的。你现在可以这样做:
df['your_column'].astype('Int64').astype('str')
And it will properly convert 1.0
to 1
.
它会正确地转换1.0
为1
.
For older pandas versions:
对于较旧的熊猫版本:
If you do not want to change the displayoptions of all pandas, @maxymoo solution does, you can use apply
:
如果您不想更改所有熊猫的显示选项,@maxymoo 解决方案可以,您可以使用apply
:
df['your_column'].apply(lambda x: f'{x:.0f}')