簡體   English   中英

在 pd.dataframe 中使用“//”

[英]Using '//' in pd.dataframe

這是來自 Pandas Pydata - https://pandas.pydata.org/docs/user_guide/reshaping.html#combining-with-stats-and-groupby的示例

有人可以解釋一下dataframe中//的用途嗎?

df = cols + pd.DataFrame(
    (np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str)
) 

Code below:   

np.random.seed([3, 1415]  

n = 20  

cols = np.array(["key", "row", "item", "col"]) 

df = cols + pd.DataFrame(
    (np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str)
)    

df.columns = cols  

df = df.join(pd.DataFrame(np.random.rand(n, 2).round(2)).add_prefix("val"))  

//用於將大小為20 x 4的隨機數組除以[2, 1, 2, 1]作為 integer 數組。

import pandas as pd
import numpy as np

n = 20
data = pd.DataFrame(
    (np.random.randint(5, size=(n, 4)) // [2, 1, 2, 1]).astype(str))
print("Integer division data")
print(data)

print("Float division data")
data = pd.DataFrame(
    (np.random.randint(5, size=(n, 4)) / [2, 1, 2, 1]).astype(str))

Output:

Integer division data
    0  1  2  3
0   0  2  1  2
1   1  2  1  2
2   1  1  2  4
3   1  2  2  1
4   2  1  1  3
5   2  2  1  4
6   2  4  1  0
7   1  3  1  3
8   0  3  0  0
9   1  3  0  1
10  1  0  1  1
11  1  2  0  1
12  1  3  1  1
13  0  2  2  0
14  1  2  1  4
15  0  3  1  0
16  2  3  0  1
17  1  0  0  4
18  2  1  1  3
19  0  1  2  0
Float division data
      0    1    2    3
0   2.0  3.0  0.0  3.0
1   2.0  4.0  0.5  1.0
2   1.0  0.0  1.0  3.0
3   1.5  1.0  1.5  3.0
4   2.0  0.0  1.0  2.0
5   0.5  3.0  2.0  0.0
6   0.5  0.0  0.0  2.0
7   1.0  0.0  1.5  2.0
8   2.0  4.0  0.5  2.0
9   0.5  4.0  0.5  3.0
10  1.0  1.0  0.5  3.0
11  2.0  1.0  2.0  1.0
12  1.5  4.0  0.0  1.0
13  0.5  4.0  1.5  2.0
14  1.5  3.0  2.0  2.0
15  0.0  0.0  1.0  3.0
16  0.5  4.0  1.0  2.0
17  1.5  0.0  2.0  4.0
18  1.5  1.0  0.5  3.0
19  0.5  2.0  0.5  2.0

簡短的例子:

4/3
# 1.3333333333333333
4//3
# 1

暫無
暫無

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

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