连接重复值/行时出现问题

2024-03-29 12:51:45 发布

您现在位置:Python中文网/ 问答频道 /正文

python新手,似乎不知道如何继续。 在使用bin并编辑了我的数据框之后,我想到了:

    Continents  % Renewable Country
0   Asia    (15.753, 29.227]    China
1   North America   (2.212, 15.753] United States
2   Asia    (2.212, 15.753] Japan
3   Europe  (2.212, 15.753] United Kingdom
4   Europe  (15.753, 29.227]    Russian Federation
5   North America   (56.174, 69.648]    Canada
6   Europe  (15.753, 29.227]    Germany
7   Asia    (2.212, 15.753] India
8   Europe  (15.753, 29.227]    France
9   Asia    (2.212, 15.753] South Korea
10  Europe  (29.227, 42.701]    Italy
11  Europe  (29.227, 42.701]    Spain
12  Asia    (2.212, 15.753] Iran
13  Australia   (2.212, 15.753] Australia
14  South America   (56.174, 69.648]    Brazil

现在,当我将大陆和%可再生能源设定为多重指标时,使用:

Top15 = Top15.groupby(by=['Continents', '% Renewable']).sum()

获取以下内容:

                            Country
Continents  % Renewable   
Asia    (15.753, 29.227]    China
         (2.212, 15.753]    JapanIndiaSouth KoreaIran
Australia   (2.212, 15.753] Australia
Europe  (15.753, 29.227]    Russian FederationGermanyFrance
        (2.212, 15.753] United Kingdom
        (29.227, 42.701]    ItalySpain
North America   (2.212, 15.753] United States
                 (56.174, 69.648]   Canada
South America   (56.174, 69.648]    Brazil

现在我想有一个专栏,可以告诉我每个索引中的国家数,即:

第一排-中国=1

排在第二位的是日本、韩国和伊朗

所以最后我想要这样的东西:

Asia           (2.212, 15.753]     4
               (15.753, 29.227]    1

我只是不知道怎么去那里。你知道吗

此外,数字需要按降序排序,同时仍然保持索引分组。你知道吗


Tags: countryunitedkingdomsouthstateseuropechinanorth
1条回答
网友
1楼 · 发布于 2024-03-29 12:51:45

含有size的溶液:

What is the difference between size and count in pandas?

print (Top15.groupby(['Continents', '% Renewable']).size())
Name: Country, dtype: int64
Continents     % Renewable     
Asia           (15.753, 29.227]    1
               (2.212, 15.753]     4
Australia      (2.212, 15.753]     1
Europe         (15.753, 29.227]    3
               (2.212, 15.753]     1
               (29.227, 42.701]    2
North America  (2.212, 15.753]     1
               (56.174, 69.648]    1
South America  (56.174, 69.648]    1
dtype: int64

如果需要更改顺序,则使用^{};对于数据帧add ^{},如果需要,则使用last MultiIndexadd ^{}

print (Top15.groupby(['Continents', '% Renewable']) \
            .size() \
            .reset_index(name='COUNT') \
            .sort_values(['Continents', 'COUNT'], ascending=[True, False]) \
            .set_index(['Continents','% Renewable']).COUNT)

Continents     % Renewable     
Asia           (2.212, 15.753]     4
               (15.753, 29.227]    1
Australia      (2.212, 15.753]     1
Europe         (15.753, 29.227]    3
               (29.227, 42.701]    2
               (2.212, 15.753]     1
North America  (2.212, 15.753]     1
               (56.174, 69.648]    1
South America  (56.174, 69.648]    1
Name: COUNT, dtype: int64
网友
2楼 · 发布于 2024-03-29 12:51:45
Top15.groupby(['Continents', '% Renewable']).Country.count()

Continents     % Renewable     
Asia           (15.753, 29.227]    1
               (2.212, 15.753]     4
Australia      (2.212, 15.753]     1
Europe         (15.753, 29.227]    3
               (2.212, 15.753]     1
               (29.227, 42.701]    2
North America  (2.212, 15.753]     1
               (56.174, 69.648]    1
South America  (56.174, 69.648]    1
Name: Country, dtype: int64

按你喜欢的顺序排序

Top15_count = Top15.groupby(['Continents', '% Renewable']).Country.count()

Top15_count.reset_index() \
    .sort_values(
        ['Continents', 'Country'],
        ascending=[True, False]
    ).set_index(['Continents', '% Renewable']).Country

Continents     % Renewable     
Asia           (2.212, 15.753]     4
               (15.753, 29.227]    1
Australia      (2.212, 15.753]     1
Europe         (15.753, 29.227]    3
               (29.227, 42.701]    2
               (2.212, 15.753]     1
North America  (2.212, 15.753]     1
               (56.174, 69.648]    1
South America  (56.174, 69.648]    1
Name: Country, dtype: int64

相关问题 更多 >