簡體   English   中英

在python熊貓中,q中是否有類似xbar的東西?

[英]in python pandas, is there anything like xbar in q?

對於像

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中有這樣的東西嗎? 謝謝!

您可以使用以下列表理解

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類型。

您在找這個嗎?

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)

您可以簡單地map列表,如下所示:

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

或者,如果您使用的是熊貓(如您的問題所示),請使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM