【问题标题】:Custom Comparison to create new Pandas dataframe after Comparison自定义比较以在比较后创建新的 Pandas 数据框
【发布时间】:2020-11-22 12:15:49
【问题描述】:

我正在尝试根据与 2 个数据帧的比较来创建一个新的 Pandas 数据帧。

我已经在这里寻找类似的答案,但我没有设法得到我想要的结果。

我有这 2 个数据框:

totalDistrict = dataFrameDemography[['district_name', 'population_density']]
districtsWithRecyclingAreas = dataFrameRecyclingAreas['DISTRICT']

我使用 Pandas 从 2 个不同的 CSV 文件中提取我想要的列。表格如下所示:

             district_name  population_density
0                    Centro           25340.69
1                Arganzuela           23306.44
2                    Retiro           21867.53
3                 Salamanca           26830.78
4                 Chamartín           15723.25
5                    Tetuan           28664.25
6                  Chamberí           29049.26
7       Fuencarral-El Pardo            1003.00
8           Moncloa-Aravaca            2515.26
9                    Latina            9183.75
10              Carabanchel           17316.88
11                    Usera           17535.32
12       Puente de Vallecas           15345.01
13                Moratalaz           15493.59
14            Ciudad Lineal           18455.56
15                Hortaleza            6973.33
16               Villaverde            7059.13
17        Villa de Vallecas            2026.82
18                Vicálvaro            1981.11
19      San Blas-Canillejas            6934.37
20                  Barajas            1076.06
0              ARGANZUELA
1                 BARAJAS
2             CARABANCHEL
3               CHAMARTIN
4           CIUDAD LINEAL
5     FUENCARRAL-EL PARDO
6               HORTALEZA
7                  LATINA
8         MONCLOA-ARAVACA
9               MORATALAZ
10     PUENTE DE VALLECAS
11    SAN BLAS-CANILLEJAS
12                  USERA
13              VICALVARO
14      VILLA DE VALLECAS
15             VILLAVERDE
Name: DISTRICT, dtype: object

现在,我要做的是生成一个包含所有地区及其人口密度的数据框,并用一个额外的列指示它们是否有回收区(1 或 0 值 f.e)。

我只是不知道该怎么做,因为每个数据帧中的列名都不同,而且在第二个数据帧中,地区是用大写字母写的。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    让我们使用.str.upperdistrict_name 列转换为大写,然后使用Series.isin 查找district_name 中的元素,这些元素也出现在districtsWithRecyclingAreas 系列中:

    totalDistrict['recycling'] = totalDistrict['district_name'].str.upper()\
                                .isin(districtsWithRecyclingAreas).view('i1')
    

              district_name  population_density  recycling
    0                Centro            25340.69          0
    1            Arganzuela            23306.44          1
    2                Retiro            21867.53          0
    3             Salamanca            26830.78          0
    4             Chamartín            15723.25          0
    5                Tetuan            28664.25          0
    6              Chamberí            29049.26          0
    7   Fuencarral-El Pardo             1003.00          1
    8       Moncloa-Aravaca             2515.26          1
    9                Latina             9183.75          1
    10          Carabanchel            17316.88          1
    11                Usera            17535.32          1
    12   Puente de Vallecas            15345.01          1
    13            Moratalaz            15493.59          1
    14        Ciudad Lineal            18455.56          1
    15            Hortaleza             6973.33          1
    16           Villaverde             7059.13          1
    17    Villa de Vallecas             2026.82          1
    18            Vicálvaro             1981.11          0
    19  San Blas-Canillejas             6934.37          1
    20              Barajas             1076.06          1
    

    【讨论】:

    • @JavierAlonsoDelgado 请注意……您可以放心地忽略该警告……可能您使用的是旧版 pandas。
    • 打印结果时,所有条目都有0个回收区
    • 答案在您提供的示例数据上是否正常工作?
    • 我正在使用主帖中提供的数据,但没有得到您在答案中提供的结果
    • 对我来说它在示例数据上正常工作。顺便说一句,你的熊猫版本是什么?
    猜你喜欢
    • 2019-02-23
    • 2017-05-17
    • 2020-11-25
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2019-12-20
    相关资源
    最近更新 更多