CẬP NHẬT: Câu trả lời ban đầu đếm những hàng có chứa chuỗi con.
Để đếm tất cả các lần xuất hiện của một chuỗi con, bạn có thể sử dụng
Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
7:In [21]: df = pd.DataFrame[['hello', 'world', 'hehe'], columns=['words']]
In [22]: df.words.str.count["he|wo"]
Out[22]:
0 1
1 1
2 2
Name: words, dtype: int64
In [23]: df.words.str.count["he|wo"].sum[]
Out[23]: 4
Phương pháp
Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
8 chấp nhận biểu thức chính quy:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
Ví dụ:
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
Để đếm những sự xuất hiện, bạn chỉ có thể tổng hợp chuỗi Boolean này:
In [15]: df.words.str.contains[r'he|wo'].sum[]
Out[15]: 2
In [16]: df.words.str.contains[r'he'].sum[]
Out[16]: 1
Pandas iloc method to select rows and NumPy’s nan to add the missing values to these rows that we selected. In the next section, we will count the occurrences including the 10 missing values we added, above.
Pandas Count Unique Values and Missing Values in a Column
if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[250,250],'marsja_se-large-leaderboard-2','ezslot_4',156,'0','0']]};__ez_fad_position['div-gpt-ad-marsja_se-large-leaderboard-2-0'];Here’s a code example to get the number of unique values as well as how many missing values there are:
# Counting occurences as well as missing values: df_na['sex'].value_counts[dropna=False]
Code language: Python [python]
- Save
Looking at the output we can see that there are 10 missing values [yes, yes, we already knew that!].
Getting the Relative Frequencies of the Unique Values
Now that we have counted the unique values in a column we will continue by using another parameter of the value_counts[]
method: normalize
. Here’s how we get the relative frequencies of men and women in the dataset:if[typeof ez_ad_units != 'undefined']{ez_ad_units.push[[[300,250],'marsja_se-leader-1','ezslot_5',157,'0','0']]};__ez_fad_position['div-gpt-ad-marsja_se-leader-1-0'];
Code language: Python [python]
df['sex'].value_counts[normalize=True]
Điều này có thể hữu ích nếu chúng ta không chỉ muốn đếm các lần xuất hiện mà còn muốn biết, ví dụ: Bao nhiêu phần trăm của mẫu là nam và nữ. Trước khi chuyển sang phần tiếp theo, hãy để Lừa nhận được một số thống kê mô tả về cột tuổi bằng cách sử dụng phương pháp
In [15]: df.words.str.contains[r'he|wo'].sum[]
Out[15]: 2
In [16]: df.words.str.contains[r'he'].sum[]
Out[16]: 1
5:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
0Đương nhiên, đếm tuổi như chúng ta đã làm trước đó, với cột chứa giới tính, sẽ không cung cấp bất kỳ thông tin hữu ích nào. & NBSP; Ở đây, đầu ra dữ liệu từ mã trên:
Chúng ta có thể thấy rằng có 5226 giá trị của dữ liệu tuổi, giá trị trung bình là 23,85 và độ lệch chuẩn là 8,32. Đương nhiên, đếm các giá trị duy nhất của cột tuổi sẽ tạo ra rất nhiều đau đầu, nhưng tất nhiên, nó có thể tồi tệ hơn. Trong ví dụ tiếp theo, chúng ta sẽ xem xét tuổi đếm và làm thế nào chúng ta có thể xử lý dữ liệu. Điều này rất hữu ích nếu chúng tôi muốn đếm, ví dụ: dữ liệu liên tục. & nbsp;
Tạo thùng khi đếm các giá trị riêng biệt & nbsp;
Một tính năng thú vị khác của phương pháp
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
4 là chúng ta có thể sử dụng phương pháp để xác định dữ liệu liên tục thành các khoảng thời gian riêng biệt. Ở đây, cách chúng tôi đặt các thùng tham số thành một số nguyên đại diện cho số lượng In [15]: df.words.str.contains[r'he|wo'].sum[]
Out[15]: 2
In [16]: df.words.str.contains[r'he'].sum[]
Out[16]: 1
7 để tạo thùng:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
1Đối với mỗi thùng, phạm vi của các giá trị tuổi [tính theo năm, tự nhiên] là như nhau. Một người chứa độ tuổi từ 11,45 đến 22,80, là khoảng 10,855. Mặt khác, thùng tiếp theo chứa độ tuổi từ 22,80 đến 33,60, là phạm vi 11,8. Trong ví dụ này, bạn có thể thấy rằng tất cả các phạm vi ở đây gần giống nhau [tất nhiên là ngoại trừ lần đầu tiên]. Tuy nhiên, mỗi phạm vi giá trị tuổi có thể chứa một số lượng khác nhau của số người trong độ tuổi này. Chúng ta có thể thấy rằng hầu hết mọi người, bị bắt giữ dưới 22,8, tiếp theo là dưới 33,6. Nó có ý nghĩa, trong trường hợp này, phải không? Trong phần tiếp theo, chúng ta sẽ xem xét cách chúng ta có thể sử dụng đếm các giá trị duy nhất trong tất cả các cột trong một khung dữ liệu.
Đếm tần số xuất hiện trên nhiều cột
Đương nhiên, cũng có thể đếm các lần xuất hiện trong nhiều cột bằng phương pháp
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
4. Bây giờ, chúng tôi sẽ bắt đầu bằng cách tạo một khung dữ liệu từ một từ điển:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
2Như bạn có thể thấy trong đầu ra, ở trên, chúng tôi có một tập dữ liệu nhỏ hơn giúp dễ dàng hiển thị cách đếm tần số của các giá trị duy nhất trong tất cả các cột. Nếu bạn cần, bạn cũng có thể chuyển đổi một mảng numpy thành một khung dữ liệu gấu trúc. Điều đó nói rằng, ở đây, cách sử dụng phương thức Ứng dụng []:
Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
3Những gì chúng tôi đã làm, trong ví dụ mã ở trên, là sử dụng phương thức với phương thức value_counts làm tham số duy nhất. Điều này sẽ áp dụng phương pháp này cho tất cả các cột trong Pandas DataFrame. Tuy nhiên, đây thực sự không phải là một cách tiếp cận khả thi nếu chúng ta có bộ dữ liệu lớn hơn. Trên thực tế, số lượng duy nhất mà chúng tôi nhận được cho bộ dữ liệu khá nhỏ này không thể đọc được:
Đếm các sự xuất hiện của một giá trị cụ thể trong Pandas DataFrame
Tất nhiên, đó cũng có thể nhận được số lần một giá trị nhất định xuất hiện trong một cột. Ở đây, cách sử dụng gấu trúc
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
4, một lần nữa, để đếm các sự xuất hiện của một giá trị cụ thể trong một cột:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
4Trong ví dụ trên, chúng tôi đã sử dụng bộ dữ liệu mà chúng tôi đã nhập trong đoạn mã đầu tiên [tức là, bắt giữ.csv]. Hơn nữa, chúng tôi đã chọn cột chứa giới tính và sử dụng phương thức value_counts []. Bởi vì chúng tôi muốn đếm các sự xuất hiện của một giá trị nhất định, sau đó chúng tôi đã chọn nam. Đầu ra cho chúng ta thấy rằng có 4783 lần xuất hiện của giá trị nhất định này trong cột.
Như thường lệ, khi làm việc với các ngôn ngữ lập trình, có nhiều cách tiếp cận hơn một để giải quyết vấn đề. Do đó, trong ví dụ tiếp theo, chúng ta sẽ xem xét một số phương pháp thay thế liên quan đến việc nhóm dữ liệu theo danh mục bằng phương thức g phải groupBy []. & NBSP;
Đếm tần số của các lần xuất hiện trong một cột bằng phương pháp Groupby của Pandas
Trong phần này, chúng ta sẽ học cách đếm tần suất xuất hiện giữa các nhóm khác nhau. Ví dụ: chúng ta có thể sử dụng
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
5 để đếm số lần xuất hiện trong một cột:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
5Một phương pháp khác để có được tần số chúng ta có thể sử dụng là phương thức
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
6:Definition: df.words.str.contains[self, pat, case=True, flags=0, na=nan]
Docstring:
Check whether given pattern is contained in each string in the array
Parameters
----------
pat : string
Character sequence or regular expression
case : boolean, default True
If True, case sensitive
flags : int, default 0 [no flags]
re module flags, e.g. re.IGNORECASE
na : default NaN, fill value for missing values.
6Bây giờ, trong cả hai ví dụ ở trên, chúng tôi đã sử dụng dấu ngoặc để chọn cột chúng tôi muốn áp dụng phương thức trên. Giống như trong các ví dụ
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
4 mà chúng ta đã thấy trước đó. Lưu ý rằng điều này tạo ra đầu ra chính xác giống như sử dụng phương thức trước đó và để giữ cho mã của bạn sạch, tôi khuyên bạn nên sử dụng ____ 24. & nbsp; Cuối cùng, cũng đáng nói rằng sử dụng phương thức In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
6 sẽ tạo ra số lượng duy nhất, được nhóm lại cho mỗi cột . Đây rõ ràng là thông tin dư thừa:Kết luận: Số lượng gấu trúc xảy ra trong cột
Trong hướng dẫn của gấu trúc này, bạn đã học được cách đếm các lần xuất hiện trong một cột bằng cách sử dụng 1]
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
4 và 2] >>> s = pd.Series[['A', 'B', 'Aaba', 'Baca', np.nan, 'CABA', 'cat']] >>> s.str.count['a'] 0 0.0 1 0.0 2 2.0 3 2.0 4 NaN 5 0.0 6 1.0 dtype: float646 cùng với
In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
5 và In [11]: df = pd.DataFrame[['hello', 'world'], columns=['words']]
In [12]: df
Out[12]:
words
0 hello
1 world
In [13]: df.words.str.contains[r'[hw]']
Out[13]:
0 True
1 True
Name: words, dtype: bool
In [14]: df.words.str.contains[r'he|wo']
Out[14]:
0 True
1 True
Name: words, dtype: bool
6. Cụ thể, bạn đã học được cách có được tần suất xuất hiện theo thứ tự tăng dần và giảm dần, bao gồm các giá trị bị thiếu, tính toán các tần số tương đối và đánh giá các giá trị được tính. & NBSP;