在python pandas中,有什么像qbar中的xbar?

时间:2014-04-02 08:48:32

标签: python pandas

类似

x=[1.2 3.1 4.1 5.2 3.1], in q, there is 
xbar(x,1) 

将其变为

 [1 3 4 5 3]

在python pandas中有这样的东西吗?谢谢!

3 个答案:

答案 0 :(得分:0)

您可以使用list comprehension,如下所示:

from math import floor

x = [1.2, 3.1, 4.1, 5.2, 3.1]

y = [floor(i) for i in x]
z = [int(i) for i in x]

print(y)
# [1.0, 3.0, 4.0, 5.0, 3.0]
print(z)
# [1, 3, 4, 5, 3]

两个列表推导都遍历x并创建一个新列表。对于y,调用floor方法,该方法返回一个已向下舍入到最接近的整数的浮点数。对于zint方法会将其向下舍入为最接近的整数,并转换为int类型。

答案 1 :(得分:0)

你在找这个吗?

import pandas as pd
d = {'one' : pd.Series([1.1, 2.1, 3.1, 4.1]), 'two' : pd.Series([1.1, 2.1, 3.1, 4.1])}
d = pd.DataFrame(d)

d = d.astype(int) # Thanks to @filmor
# Or 
d = d.applymap(int)

答案 2 :(得分:0)

您可以简单地map列表,如下所示:

In [2]: map(int,x)
Out[2]: [1, 3, 4, 5, 3]

或者如果您正在使用pandas(如您的问题所示),请使用astype方法或map

In [3]: import pandas as pd

In [4]: x = pd.Series(x)

In [5]: x
Out[5]:
0    1.2
1    3.1
2    4.1
3    5.2
4    3.1
dtype: float64

In [6]: x.astype(int)
Out[6]:
0    1
1    3
2    4
3    5
4    3
dtype: int32