Count number of counties per state using python {c

2020-07-22 18:06发布

问题:

I am troubling with counting the number of counties using famous cenus.csv data.

Task: Count number of counties in each state.

Facing comparing (I think) / Please read below?

I've tried this:

df = pd.read_csv('census.csv')
dfd = df[:]['STNAME'].unique()  //Gives out names of state

serr = pd.Series(dfd)  // converting to series (from array)

After this, i've tried using two approaches:

1:

    df[df['STNAME'] == serr] **//ERROR: series length must match**

2:

i = 0
for name in serr:                        //This generate error 'Alabama'
    df['STNAME'] == name
    for i in serr:
        serr[i] == serr[name]
        print(serr[name].count)
        i+=1

Please guide me; it has been three days with this stuff.

回答1:

Use groupby and aggregate COUNTY using nunique:

In [1]: import pandas as pd

In [2]: df = pd.read_csv('census.csv')

In [3]: unique_counties = df.groupby('STNAME')['COUNTY'].nunique()

Now the results

In [4]: unique_counties
Out[4]: 
STNAME
Alabama                  68
Alaska                   30
Arizona                  16
Arkansas                 76
California               59
Colorado                 65
Connecticut               9
Delaware                  4
District of Columbia      2
Florida                  68
Georgia                 160
Hawaii                    6
Idaho                    45
Illinois                103
Indiana                  93
Iowa                    100
Kansas                  106
Kentucky                121
Louisiana                65
Maine                    17
Maryland                 25
Massachusetts            15
Michigan                 84
Minnesota                88
Mississippi              83
Missouri                116
Montana                  57
Nebraska                 94
Nevada                   18
New Hampshire            11
New Jersey               22
New Mexico               34
New York                 63
North Carolina          101
North Dakota             54
Ohio                     89
Oklahoma                 78
Oregon                   37
Pennsylvania             68
Rhode Island              6
South Carolina           47
South Dakota             67
Tennessee                96
Texas                   255
Utah                     30
Vermont                  15
Virginia                134
Washington               40
West Virginia            56
Wisconsin                73
Wyoming                  24
Name: COUNTY, dtype: int64


回答2:

juanpa.arrivillaga has a great solution. However, the code needs a minor modification.

The "counties" with 'SUMLEV' == 40 or 'COUNTY' == 0 should be filtered. Otherwise, all the number of counties are too big by one.

So, the correct answer should be:

unique_counties = census_df[census_df['SUMLEV'] == 50].groupby('STNAME')['COUNTY'].nunique()

with the following result:

STNAME
Alabama                  67
Alaska                   29
Arizona                  15
Arkansas                 75
California               58
Colorado                 64
Connecticut               8
Delaware                  3
District of Columbia      1
Florida                  67
Georgia                 159
Hawaii                    5
Idaho                    44
Illinois                102
Indiana                  92
Iowa                     99
Kansas                  105
Kentucky                120
Louisiana                64
Maine                    16
Maryland                 24
Massachusetts            14
Michigan                 83
Minnesota                87
Mississippi              82
Missouri                115
Montana                  56
Nebraska                 93
Nevada                   17
New Hampshire            10
New Jersey               21
New Mexico               33
New York                 62
North Carolina          100
North Dakota             53
Ohio                     88
Oklahoma                 77
Oregon                   36
Pennsylvania             67
Rhode Island              5
South Carolina           46
South Dakota             66
Tennessee                95
Texas                   254
Utah                     29
Vermont                  14
Virginia                133
Washington               39
West Virginia            55
Wisconsin                72
Wyoming                  23
Name: COUNTY, dtype: int64


回答3:

@Bakhtawar - This is a very simple way:

df.groupby(df['STNAME']).count().COUNTY