Xin chào, tôi có một trình tự bao gồm hoạt động Đọc CSV, sau khi. csv đã được đọc, tôi đang lặp qua từng bản ghi, thêm nó vào một bản dữ liệu có thể thực hiện một số xác thực và chèn từng hàng vào SQL Server DB bằng cách sử dụng hoạt động chèn. Vấn đề là nếu có một ô trống trong. csv, nó sẽ được chèn dưới dạng chuỗi rỗng thay vì null. Tôi thấy một số người thực hiện một số câu lệnh if để kiểm tra xem một hàng có bao gồm một chuỗi rỗng hay không, sau đó thay đổi nó thành null. Câu hỏi của tôi là, có cách nào sạch hơn để xử lý việc này không?
Cảm ơn
Hầu hết thời gian dữ liệu trong thế giới thực hiếm khi rõ ràng và đồng nhất. Trong nhiều trường hợp, tập dữ liệu quan tâm sẽ bị thiếu một số lượng dữ liệu. Để làm cho vấn đề trở nên phức tạp hơn, các nguồn dữ liệu khác nhau có thể chỉ ra dữ liệu bị thiếu theo những cách khác nhau
Trong mô-đun này, chúng ta sẽ thảo luận về một số cân nhắc chung đối với dữ liệu bị thiếu, thảo luận về cách Pandas chọn để thể hiện dữ liệu đó và trình bày một số công cụ Pandas tích hợp để xử lý dữ liệu bị thiếu trong Python. Chúng tôi coi dữ liệu bị thiếu là giá trị null, NaN hoặc NA nói chung
Trước khi chúng tôi bắt đầu, hãy đảm bảo rằng các gói Pandas và matplotlib đã được cài đặt
!pip install pandas matplotlib
____1# Now import pandas into your notebook as pd
import pandas as pd
Bây giờ, hãy nhập lại bộ dữ liệu
# Now import pandas into your notebook as pd
import pandas as pd
7 vào sổ ghi chép của chúng ta như chúng ta đã làm trong bài học trướcsurveys_df = pd.read_csv["surveys.csv"]
Sử dụng mặt nạ để xác định một tình trạng cụ thể
Mặt nạ có thể hữu ích để xác định vị trí tồn tại hoặc không tồn tại một tập hợp con cụ thể các giá trị - ví dụ: giá trị NaN hoặc "Không phải là số". Để hiểu mặt nạ, chúng ta cũng cần hiểu các đối tượng
# Now import pandas into your notebook as pd
import pandas as pd
8 trong PythonCác giá trị Boolean bao gồm
# Now import pandas into your notebook as pd
import pandas as pd
9 hoặc surveys_df = pd.read_csv["surveys.csv"]
0. Ví dụ,# set value of x to be 5
x = 5
______9
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
0
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
1
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
2Tìm các giá trị còn thiếu
Hãy xác định tất cả các vị trí trong dữ liệu khảo sát có giá trị dữ liệu null [thiếu hoặc NaN]. Chúng ta có thể sử dụng phương pháp
surveys_df = pd.read_csv["surveys.csv"]
1 để làm điều này. Phương thức surveys_df = pd.read_csv["surveys.csv"]
1 sẽ so sánh từng ô với giá trị null. Nếu một phần tử có giá trị null, nó sẽ được gán giá trị là # Now import pandas into your notebook as pd
import pandas as pd
9 trong đối tượng đầu ra
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
6record_idmonthdayyearsite_idspecies_idsexhindfoot_lengthweight0FalseFalseFalseFalseFalseFalseFalseFalseTrue1FalseFalseFalseFalseFalseFalseFalseFalseTrue2FalseFalseFalseFalseFalseFalseFalseFalseTrue3FalseFalseFalseFalseFalseFalseFalseFalseTrue4FalseFalseFalseFalseFalseFalseFalseFalseTrue
Cách chọn hàng thiếu dữ liệu
Để chọn các hàng có giá trị null, chúng ta có thể sử dụng mặt nạ làm chỉ mục để tập hợp dữ liệu của mình như sau
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
0record_idmonthdayyearsite_idspecies_idsexhindfoot_lengthweight0171619772NLM32. 0NaN1271619773NLM33. 0NaN2371619772DMF37. 0NaN3471619777DMM36. 0NaN4571619773DMM35. 0NaN5671619771PFM14. 0NaN6771619772PEFNaNNaN7871619771DMM37. 0NaN8971619771DMF34. 0NaN91071619776PFF20. 0NaN101171619775DSF53. 0NaN111271619777DMM38. 0NaN121371619773DMM35. 0NaN131471619778DMNaNNaNNaN141571619776DMF36. 0NaN151671619774DMF36. 0NaN161771619773DSF48. 0NaN171871619772PPM22. 0NaN181971619774PFNaNNaNNaN1920717197711DSF48. 0NaN2021717197714DMF34. 0NaN2122717197715NLF31. 0NaN2223717197713DMM36. 0NaN2324717197713SHM21. 0NaN242571719779DMM35. 0NaN2526717197715DMM31. 0NaN2627717197715DMM36. 0NaN2728717197711DMM38. 0NaN2829717197711PPMNaNNaN2930717197710DSF52. 0NaN. 35187351881110200210NaNNaNNaNNaN3525635257127200222PBM26. 0NaN3525935260127200221PBF24. 0NaN3527735278127200220AHNaNNaNNaN3527935280127200216PBM28. 0NaN3532235323128200211AHNaNNaNNaN3532835329128200211PPMNaN16. 03537035371128200214AHNaNNaNNaN3537835379128200215PBF26. 0NaN3538435385128200210NaNNaNNaNNaN3538735388122920021DOM35. 0NaN3540335404122920022NLF30. 0NaN35448354491229200220OTF20. 0NaN35452354531229200220PBM28. 0NaN35457354581229200220AHNaNNaNNaN35477354781229200224AHNaNNaNNaN35485354861229200216DOM37. 0NaN3549535496123120024PBNaNNaNNaN35510355111231200211DXNaNNaNNaN35511355121231200211USNaNNaNNaN35512355131231200211USNaNNaNNaN3551435121501231SFF207231SFF. 0NaN3551935520123120029SFNaN24. 036. 035527355281231200213USNaNNaNNaN35529355301231200213OTF20. 0NaN35530355311231200213PBF27. 0NaN35543355441231200215USNaNNaNNaN35544355451231200215AHNaNNaNNaN35545355461231200215AHNaNNaNNaN3554835549123120025NaNNaNNaNNaN
4873 hàng × 9 cột
Giải trình
Lưu ý rằng chúng tôi có
surveys_df = pd.read_csv["surveys.csv"]
4 quan sát/hàng chứa một hoặc nhiều giá trị bị thiếu. Đó là khoảng surveys_df = pd.read_csv["surveys.csv"]
5 dữ liệu chứa các giá trị bị thiếuChúng tôi đã sử dụng quy ước
surveys_df = pd.read_csv["surveys.csv"]
6 để chọn tập hợp con dữ liệuThông tin thêm về cắt và lập chỉ mục có thể được tìm thấy ở đây
[trục=1] là quy ước gọn gàng để chỉ định các cột
Lưu ý rằng cột
surveys_df = pd.read_csv["surveys.csv"]
7 trong Khung dữ liệu của chúng tôi chứa nhiều giá trị surveys_df = pd.read_csv["surveys.csv"]
8 hoặc surveys_df = pd.read_csv["surveys.csv"]
9. Tiếp theo, chúng ta sẽ khám phá các cách đối phó với điều nàyNếu chúng ta nhìn vào cột
surveys_df = pd.read_csv["surveys.csv"]
7 trong dữ liệu khảo sát, chúng ta sẽ nhận thấy rằng có các giá trị NaN [Không phải là Số]. Giá trị NaN là giá trị không xác định không thể được biểu diễn bằng toán học. Ví dụ, gấu trúc sẽ đọc một ô trống trong trang tính CSV hoặc Excel dưới dạng NaN. NaN có một số thuộc tính mong muốn. nếu chúng ta tính trung bình cột surveys_df = pd.read_csv["surveys.csv"]
7 mà không thay thế các NaN của mình, Python sẽ biết bỏ qua các ô đóXử lý các giá trị còn thiếu
Xử lý các giá trị dữ liệu bị thiếu luôn là một thách thức. Đôi khi rất khó để biết tại sao các giá trị bị thiếu - có phải do lỗi nhập dữ liệu không? . Nếu may mắn, chúng tôi có một số siêu dữ liệu sẽ cho chúng tôi biết thêm về cách xử lý các giá trị null
Chẳng hạn, trong một số lĩnh vực, như Viễn thám, các giá trị dữ liệu bị thiếu thường được định nghĩa là -9999. Có một loạt các giá trị -9999 trong dữ liệu của bạn thực sự có thể thay đổi các phép tính số. Thông thường trong bảng tính, các ô bị bỏ trống khi không có dữ liệu. Theo mặc định, gấu trúc sẽ thay thế các giá trị bị thiếu đó bằng NaN. Tuy nhiên, bạn nên tập thói quen đánh dấu có chủ ý các ô không có dữ liệu, không có giá trị dữ liệu. Bằng cách đó, sẽ không có câu hỏi nào trong tương lai khi bạn [hoặc người khác] khám phá dữ liệu của bạn
NaN ở đâu?
Hãy khám phá thêm một chút về các giá trị NaN trong dữ liệu của chúng tôi. Sử dụng các công cụ đã học ở bài 02, chúng ta có thể tính xem có bao nhiêu hàng chứa giá trị NaN cho trọng số. Chúng tôi cũng có thể tạo một tập hợp con mới từ dữ liệu của mình chỉ chứa các hàng có giá trị trọng số > 0 [i. e. , chọn giá trị trọng số có ý nghĩa]
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
1
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
2
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
3
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
4Chúng ta có thể thay thế tất cả các giá trị NaN bằng 0 bằng cách sử dụng phương pháp ________ 82 [sau khi sao chép dữ liệu để không mất công]
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
5Tuy nhiên NaN và 0 mang lại kết quả phân tích khác nhau. Giá trị trung bình khi các giá trị NaN được thay thế bằng 0 khác với khi các giá trị NaN bị loại bỏ hoặc bỏ qua một cách đơn giản
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
6
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
7____18
Requirement already satisfied: pandas in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [0.25.0]
Requirement already satisfied: matplotlib in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [3.1.1]
Requirement already satisfied: numpy>=1.13.3 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [1.17.0]
Requirement already satisfied: python-dateutil>=2.6.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2.8.0]
Requirement already satisfied: pytz>=2017.2 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from pandas] [2019.1]
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [2.4.1.1]
Requirement already satisfied: cycler>=0.10 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [0.10.0]
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from matplotlib] [1.1.0]
Requirement already satisfied: six>=1.5 in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from python-dateutil>=2.6.1->pandas] [1.12.0]
Requirement already satisfied: setuptools in /Users/perry/.virtualenvs/python-workshop-base-ufuVBSbV/lib/python3.6/site-packages [from kiwisolver>=1.0.1->matplotlib] [39.1.0]
9Chúng tôi có thể điền các giá trị NaN với bất kỳ giá trị nào mà chúng tôi đã chọn. Mã bên dưới điền vào tất cả các giá trị NaN với giá trị trung bình cho tất cả các giá trị trọng số
# Now import pandas into your notebook as pd
import pandas as pd
0____18____17Viết dữ liệu ra CSV
Chúng ta đã học về cách sử dụng thao tác dữ liệu để có được kết quả đầu ra mong muốn. Nhưng chúng tôi cũng đã thảo luận về việc giữ dữ liệu đã được thao tác tách biệt khỏi dữ liệu thô của chúng tôi. Một cái gì đó chúng tôi có thể quan tâm đến là chỉ làm việc với các cột có đầy đủ dữ liệu. Trước tiên, hãy tải lại dữ liệu để chúng tôi không trộn lẫn tất cả các thao tác trước đây của mình
# Now import pandas into your notebook as pd
import pandas as pd
3Nếu bây giờ bạn nhập
# set value of x to be 5
x = 5
3, bạn sẽ quan sát thấy rằng DataFrame kết quả có 84 hàng và 9 cột, nhỏ hơn nhiều so với bản gốc _______85 hàngBây giờ chúng ta có thể sử dụng lệnh
# set value of x to be 5
x = 5
6 để xuất DataFrame ở định dạng CSV. Lưu ý rằng mã bên dưới sẽ theo mặc định lưu dữ liệu vào thư mục làm việc hiện tại. Chúng tôi có thể lưu nó vào một thư mục khác bằng cách thêm tên thư mục và dấu gạch chéo trước tên tệp. # set value of x to be 5
x = 5
7. Chúng tôi sử dụng 'index=False' để gấu trúc không bao gồm số chỉ mục cho mỗi dòng# Now import pandas into your notebook as pd
import pandas as pd
4Tóm tắt lại
Những gì chúng tôi đã học được
- Giá trị NaN là gì, cách chúng có thể được biểu diễn và điều này có ý nghĩa gì đối với công việc của bạn
- Cách thay thế các giá trị NaN, nếu muốn
- Cách sử dụng
6 để ghi dữ liệu đã thao tác vào một tệp# set value of x to be 5 x = 5
Chúng tôi cũng có thể chạy
surveys_df = pd.read_csv["surveys.csv"]
1 trên một cột cụ thể. Mã dưới đây làm gì?# Now import pandas into your notebook as pd
import pandas as pd
5# Now import pandas into your notebook as pd
import pandas as pd
6Chúng ta hãy dành một phút để xem xét tuyên bố trên. Chúng tôi đang sử dụng đối tượng Boolean
x > 5
0 làm chỉ mục cho x > 5
1. Chúng tôi đang yêu cầu Python chọn các hàng có giá trị trọng lượng surveys_df = pd.read_csv["surveys.csv"]
9