按比例劃分DataFrame


Pandas庫用於資料操作和分析。資料將透過兩種方式使用pandas庫建立:DataFrame和Series。DataFrame是包含行和列的二維資料結構。

根據比例劃分DataFrame資料的方法有很多。讓我們一一來看。

  • 使用np.random.rand()

  • 使用pandas.DataFrame.sample()

  • 使用numpy.split()

使用numpy.random.rand()

在下面的示例中,我們將使用randm.rand()函式透過定義比例將dataframe資料分成幾部分。如果我們想按60%和40%的比例劃分資料,我們將比例定義為0.6和0.4。

import numpy as np
ratio = np.random.rand(dataframe)
dataframe[ratio comparision_operator value]

示例

在下面的示例中,我們將使用randm.rand()函式透過定義比例將dataframe資料分成幾部分。如果我們想按60%和40%的比例劃分資料,我們將比例定義為0.6和0.4。

import numpy as np
import pandas as pd
data=pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")
data = data[:20]
ratio = np.random.rand(len(data))
train_data = data[ratio < 0.6]
train_data.head()
test_data = data[ratio >= 0.6]
test_data.head()

輸出

    PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
4             5         0       3  ...   8.0500   NaN         S
8             9         1       3  ...  11.1333   NaN         S
10           11         1       3  ...  16.7000    G6         S
14           15         0       3  ...   7.8542   NaN         S
17           18         1       2  ...  13.0000   NaN         S

[5 rows x 12 columns]

使用pandas.DataFrame.sample()

另一種按比例劃分DataFrame的方法是使用sample()函式與DataFrame一起使用。它接受兩個引數:frac用於定義分數,random_state用於獲取隨機數生成器的種子值。

語法

以下是語法。

dataframe.sample(frac,random_state)

示例

在下面的示例中,我們使用pandas庫中提供的sample()函式,將資料按50%和50%的比例分成兩部分。

import pandas as pd
dic = {"Letters":['A','B','C','D','E','F','G','H'],
      "Number":[1,2,3,4,5,6,7,8]}
data = pd.DataFrame(dic)
print("The Original data:")
print(data)
print("The 50% of the original data")
train_data = data.sample(frac = 0.5, random_state = 40)
print(train_data)
print("Another 50% of the data")
test_data = data.drop(train_data.index)
print(test_data)

輸出

The Original data:
  Letters  Number
0       A       1
1       B       2
2       C       3
3       D       4
4       E       5
5       F       6
6       G       7
7       H       8
The 50% of the original data
  Letters  Number
7       H       8
1       B       2
2       C       3
4       E       5
Another 50% of the data
  Letters  Number
0       A       1
3       D       4
5       F       6
6       G       7

使用numpy.split()函式

另一種基於比例劃分dataframe的方法是numpy.split()。在Numpy庫中,我們有split()函式,它接受dataframe以及比例*dataframe長度。

語法

以下是語法。

numpy.split(dataframe,[int(ratio*len(dataframe))

示例

下面的示例使用numpy.split()函式將dataframe按70%和30%的比例劃分。

import pandas as pd
import numpy as np
dic = {"Letters":['A','B','C','D','E','F','G','H'],
      "Number":[1,2,3,4,5,6,7,8]}
data = pd.DataFrame(dic)
print("The Original data:")
print(data)
print("The 70% of the original data")
train_data, test_data= np.split(data,[int(0.7*len(data))])
print(train_data)
print("Another 30% of the data")
print(test_data)

輸出

The Original data:
  Letters  Number
0       A       1
1       B       2
2       C       3
3       D       4
4       E       5
5       F       6
6       G       7
7       H       8
The 70% of the original data
  Letters  Number
0       A       1
1       B       2
2       C       3
3       D       4
4       E       5
Another 30% of the data
  Letters  Number
5       F       6
6       G       7
7       H       8

更新於:2023年11月2日

608 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告