📜  按比例划分 DataFrame

📅  最后修改于: 2022-05-13 01:55:29.773000             🧑  作者: Mango

按比例划分 DataFrame

Pandas是一个建立在 numpy 库之上的开源库。 Dataframe是一种二维数据结构,就像数据在行和列中以表格方式对齐。 DataFrame.sample()方法可以用来划分Dataframe。

frac 属性是定义要使用的 Dataframe 分数的属性。例如 frac = 0.25 表示将使用 25% 的 Dataframe。

现在,让我们创建一个数据框:

Python3
# importing pandas as pd 
import pandas as pd
  
# dictionary
cars = {
  'Brand': ['Honda Civic', 'Toyota Corolla', 
            'Ford Focus', 'Audi A4', 'Maruti 800',
            'Toyota Innova', 'Tata Safari', 'Maruti Zen', 
            'Maruti Omni', 'Honda Jezz'],
   'Price': [22000, 25000, 27000, 35000,
             20000, 25000, 31000, 23000,
             26000, 25500]
 }
  
# create the dataframe 
df = pd.DataFrame(cars, 
                  columns = ['Brand',
                             'Price'])
# show the dataframe
df


Python3
# importing pandas as pd 
import pandas as pd
  
# dictionary
cars = {
  'Brand': ['Honda Civic', 'Toyota Corolla', 
            'Ford Focus', 'Audi A4', 'Maruti 800',
            'Toyota Innova', 'Tata Safari', 'Maruti Zen', 
            'Maruti Omni', 'Honda Jezz'],
   'Price': [22000, 25000, 27000, 35000,
             20000, 25000, 31000, 23000,
             26000, 25500]
 }
  
# create the dataframe 
df = pd.DataFrame(cars, 
                  columns = ['Brand',
                             'Price'])
  
# Print the 60% of the dataframe 
part_60 = df.sample(frac = 0.6)
print("\n 60%  DataFrame:")
print(part_60)
  
# Print the 40% of the dataframe 
part_40 = df.drop(part_60.index)
print("\n 40% DataFrame:")
print(part_40)


Python3
# importing pandas as pd 
import pandas as pd
  
# dictionary
cars = {
  'Brand': ['Honda Civic', 'Toyota Corolla', 
            'Ford Focus', 'Audi A4', 'Maruti 800',
            'Toyota Innova', 'Tata Safari', 'Maruti Zen', 
            'Maruti Omni', 'Honda Jezz'],
   'Price': [22000, 25000, 27000, 35000,
             20000, 25000, 31000, 23000,
             26000, 25500]
 }
  
# create the dataframe 
df = pd.DataFrame(cars, 
                  columns = ['Brand',
                             'Price'])
  
# Print the 80% of the dataframe 
part_80 = df.sample(frac = 0.8)
print("\n 80%  DataFrame:")
print(part_80)
  
# Print the 20% of the dataframe 
part_20 = df.drop(part_80.index)
print("\n 20% DataFrame:")
print(part_20)


输出:

数据框

示例 1:将给定的 Dataframe 分为 60% 和 40%。

Python3

# importing pandas as pd 
import pandas as pd
  
# dictionary
cars = {
  'Brand': ['Honda Civic', 'Toyota Corolla', 
            'Ford Focus', 'Audi A4', 'Maruti 800',
            'Toyota Innova', 'Tata Safari', 'Maruti Zen', 
            'Maruti Omni', 'Honda Jezz'],
   'Price': [22000, 25000, 27000, 35000,
             20000, 25000, 31000, 23000,
             26000, 25500]
 }
  
# create the dataframe 
df = pd.DataFrame(cars, 
                  columns = ['Brand',
                             'Price'])
  
# Print the 60% of the dataframe 
part_60 = df.sample(frac = 0.6)
print("\n 60%  DataFrame:")
print(part_60)
  
# Print the 40% of the dataframe 
part_40 = df.drop(part_60.index)
print("\n 40% DataFrame:")
print(part_40)

输出:

数据框 60 和 40 部分明智

示例 2:将给定的 Dataframe 分为 80% 和 20%。

Python3

# importing pandas as pd 
import pandas as pd
  
# dictionary
cars = {
  'Brand': ['Honda Civic', 'Toyota Corolla', 
            'Ford Focus', 'Audi A4', 'Maruti 800',
            'Toyota Innova', 'Tata Safari', 'Maruti Zen', 
            'Maruti Omni', 'Honda Jezz'],
   'Price': [22000, 25000, 27000, 35000,
             20000, 25000, 31000, 23000,
             26000, 25500]
 }
  
# create the dataframe 
df = pd.DataFrame(cars, 
                  columns = ['Brand',
                             'Price'])
  
# Print the 80% of the dataframe 
part_80 = df.sample(frac = 0.8)
print("\n 80%  DataFrame:")
print(part_80)
  
# Print the 20% of the dataframe 
part_20 = df.drop(part_80.index)
print("\n 20% DataFrame:")
print(part_20)

输出:

数据框 80 和 20 部分明智