迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

拆分 Pandas DataFrame

作者:迹忆客 最近更新:2024/04/21 浏览次数:

本教程解释了如何使用行索引、DataFrame.groupby() 方法和 DataFrame.sample() 方法将一个 DataFrame 分割成多个较小的 DataFrame。

我们将使用下面的 apprix_df DataFrame 来解释如何将一个 DataFrame 分割成多个更小的 DataFrame。

import pandas as pd

apprix_df = pd.DataFrame(
    {
        "Name": ["Anish", "Rabindra", "Manish", "Samir", "Binam"],
        "Post": ["CEO", "CTO", "System Admin", "Consultant", "Engineer"],
        "Qualification": ["MBA", "MS", "MCA", "PhD", "BE"],
    }
)

print("Apprix Team DataFrame:")
print(apprix_df, "\n")

输出:

Apprix Team DataFrame:
       Name          Post Qualification
0     Anish           CEO           MBA
1  Rabindra           CTO            MS
2    Manish  System Admin           MCA
3     Samir    Consultant           PhD
4     Binam      Engineer            BE

使用行索引分割 DataFrame

import pandas as pd

apprix_df = pd.DataFrame(
    {
        "Name": ["Anish", "Rabindra", "Manish", "Samir", "Binam"],
        "Post": ["CEO", "CTO", "System Admin", "Consultant", "Engineer"],
        "Qualification": ["MBA", "MS", "MCA", "PhD", "BE"],
    }
)

print("Apprix Team DataFrame:")
print(apprix_df, "\n")

apprix_1 = apprix_df.iloc[:2, :]
apprix_2 = apprix_df.iloc[2:, :]

print("The DataFrames formed by splitting of Apprix Team DataFrame are: ", "\n")
print(apprix_1, "\n")
print(apprix_2, "\n")

输出:

Apprix Team DataFrame:
       Name          Post Qualification
0     Anish           CEO           MBA
1  Rabindra           CTO            MS
2    Manish  System Admin           MCA
3     Samir    Consultant           PhD
4     Binam      Engineer            BE

The DataFrames formed by splitting the Apprix Team DataFrame are:

       Name Post Qualification
0     Anish  CEO           MBA
1  Rabindra  CTO            MS

     Name          Post Qualification
2  Manish  System Admin           MCA
3   Samir    Consultant           PhD
4   Binam      Engineer            BE

它使用行索引将 DataFrame apprix_df 分成两部分。第一部分包含 apprix_df DataFrame 的前两行,而第二部分包含最后三行。

我们可以在 iloc 属性中指定每次分割的行。[:2,:] 表示选择索引 2 之前的行(索引 2 的行不包括在内)和 DataFrame 中的所有列。因此,apprix_df.iloc[:2,:] 选择 DataFrame apprix_df 中索引 01 的前两行。


使用 groupby() 方法拆分 DataFrame

import pandas as pd

apprix_df = pd.DataFrame(
    {
        "Name": ["Anish", "Rabindra", "Manish", "Samir", "Binam"],
        "Post": ["CEO", "CTO", "System Admin", "Consultant", "Engineer"],
        "Qualification": ["MBA", "MS", "MS", "PhD", "MS"],
    }
)

print("Apprix Team DataFrame:")
print(apprix_df, "\n")

groups = apprix_df.groupby(apprix_df.Qualification)
ms_df = groups.get_group("MS")
mba_df = groups.get_group("MBA")
phd_df = groups.get_group("PhD")

print("Group with Qualification MS:")
print(ms_df, "\n")

print("Group with Qualification MBA:")
print(mba_df, "\n")

print("Group with Qualification PhD:")
print(phd_df, "\n")

输出:

Apprix Team DataFrame:
       Name          Post Qualification
0     Anish           CEO           MBA
1  Rabindra           CTO            MS
2    Manish  System Admin            MS
3     Samir    Consultant           PhD
4     Binam      Engineer            MS

Group with Qualification MS:
       Name          Post Qualification
1  Rabindra           CTO            MS
2    Manish  System Admin            MS
4     Binam      Engineer            MS

Group with Qualification MBA:
    Name Post Qualification
0  Anish  CEO           MBA

Group with Qualification PhD:
    Name        Post Qualification
3  Samir  Consultant           PhD

它根据 Qualification 列的值将 DataFrame apprix_df 分成三部分。Qualification 列值相同的行将被放在同一个组中。

groupby() 函数将根据 Qualification 列的值形成分组。然后我们使用 get_group() 方法提取被 groupby() 方法分组的行。


使用 sample() 方法拆分 DataFrame

我们可以通过使用 sample() 方法从 DataFrame 中随机抽取行来形成一个 DataFrame。我们可以设置从父 DataFrame 中抽取行的比例。

import pandas as pd

apprix_df = pd.DataFrame(
    {
        "Name": ["Anish", "Rabindra", "Manish", "Samir", "Binam"],
        "Post": ["CEO", "CTO", "System Admin", "Consultant", "Engineer"],
        "Qualification": ["MBA", "MS", "MS", "PhD", "MS"],
    }
)

print("Apprix Team DataFrame:")
print(apprix_df, "\n")

random_df = apprix_df.sample(frac=0.4, random_state=60)

print("Random split from the Apprix Team DataFrame:")
print(random_df)

输出:

Apprix Team DataFrame:
       Name          Post Qualification
0     Anish           CEO           MBA
1  Rabindra           CTO            MS
2    Manish  System Admin            MS
3     Samir    Consultant           PhD
4     Binam      Engineer            MS

Random split from the Apprix Team DataFrame:
    Name      Post Qualification
0  Anish       CEO           MBA
4  Binam  Engineer            MS

它从 apprix_df DataFrame 中随机抽取 40% 的行,然后显示由抽取的行形成的 DataFrame。设置 random_state 是为了确保每次抽样都能得到相同的随机样本。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

比较 Pandas DataFrame 对象

发布时间:2024/04/21 浏览次数:62 分类:Python

本教程介绍了我们如何在 Python 中比较 Pandas DataFrame 对象。比较 DataFrames 对检查 DataFrames 之间的差异非常有帮助。

Pandas 复制 DataFrame

发布时间:2024/04/21 浏览次数:117 分类:Python

本教程将介绍如何使用 DataFrame.copy()方法复制 DataFrame 对象,并探讨 DataFrame.copy()方法的使用。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便