使用pandas进行唯一邮政编码计数

huangapple go评论63阅读模式
英文:

Counting unique zip codes with pandas

问题

我正在尝试计算每月时间段内每个邮政编码的数量

```python
df1 = pd.DataFrame(df)
print(df[['Timestamp', 'zip']].value_counts())

我得到了以下输出:

Timestamp                 zip  
Front Desk Call Log (Master Data)  NaN      1
2023-07-21 12:22:47.697000       60191    1
2023-07-21 10:55:13.311000       NaN      1
2023-07-21 10:49:06.148000       60187    1
2023-07-21 10:29:08.396000       60189    1
                                       ..
2023-07-21 14:43:23.522000       60187    1
2023-07-21 14:45:12.332000       60440    1
2023-07-21 14:46:46.452000       NaN      1
2023-07-21 17:34:11.631000       60548    1
2023-07-21 17:39:36.358000       60133    1
Length: 8314, dtype: int64

我不需要邮政编码的数量,我需要每个邮政编码的计数。我期望的输出是:

60187 2
60542 1
60540 3

等等。

英文:

I'm trying to count the number of each zip codes within a monthly period of time frame.

df1 = pd.DataFrame(df)
print(df[['Timestamp', 'zip']].nunique())

I get an output of:

Timestamp    8314
zip           343
dtype: int64

I don't need the number of zip codes, I need the count of each zip code.
I was expecting:

60187 2
60542 1
60540 3

etc.

asked to post sample rows:

                              Timestamp    zip
0     Front Desk Call Log (Master Data)    NaN
1            2023-07-21 12:22:47.697000  60191
2            2023-07-21 10:55:13.311000    NaN
3            2023-07-21 10:49:06.148000  60187
4            2023-07-21 10:29:08.396000  60189
...                                 ...    ...
8309         2023-07-21 14:43:23.522000  60187
8310         2023-07-21 14:45:12.332000  60440
8311         2023-07-21 14:46:46.452000    NaN
8312         2023-07-21 17:34:11.631000  60548
8313         2023-07-21 17:39:36.358000  60133

[8314 rows x 2 columns]

答案1

得分: 1

使用groupby函数与value_counts结合的替代方法来实现相同的结果如下所示:

按“zip”分组并计算每个邮政编码的出现次数

df.groupby('zip').size()
英文:

An alternative solution to achieve the same result is by using the groupby function in combination with value_counts. Here's the code for the alternate solution:

Group by 'zip' and count the occurrences of each zip code

df.groupby('zip').size()

答案2

得分: 0

尝试:

df['zip'].value_counts()

如果您想要计算空值(Nulls):

df['zip'].value_counts(dropna=False)
英文:

Try:

df['zip'].value_counts()

If you want to count the Nulls as well:

df['zip'].value_counts(dropna=False)

huangapple
  • 本文由 发表于 2023年7月24日 00:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定