YouTube2Text

Learn Pandas in 1 hour! 🐼 — Transcript

by Bro Code · 8,014 words · 1,289 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey everybody. In this video, I'm going
  2. 0:01to teach you everything you need to know
  3. 0:03to start working with pandas. So, I
  4. 0:05encourage you to sit back, relax, and
  5. 0:08enjoy the show.
  6. 0:11Pandas is a Python library built on top
  7. 0:14of NumPy. It's widely used in data
  8. 0:16analysis, data science, and machine
  9. 0:18learning. The name pandas comes from the
  10. 0:20term panel data. Unfortunately, it has
  11. 0:22nothing to do with pandas, the animal.
  12. 0:24Using this library, you typically work
  13. 0:26with objects known as series and dataf
  14. 0:29frames. A series is like a
  15. 0:31one-dimensional labeled column. A dataf
  16. 0:33frame is like a two-dimensional labeled
  17. 0:35grid or table. With this library, you
  18. 0:38can import, display, manipulate, and
  19. 0:40export tabular data. Think of pandas as
  20. 0:43Python's version of Microsoft Excel, but
  21. 0:45on steroids. Before getting started with
  22. 0:48the series, I would recommend learning
  23. 0:50everything up to Python lists. I do have
  24. 0:52a free 1-hour course on YouTube to catch
  25. 0:54you up to speed if you're interested.
  26. 0:57Here's how we can start working with
  27. 0:58pandas. Within your IDE at the top,
  28. 1:02we're going to import the library of
  29. 1:05pandas, then give it an alias, as pd,
  30. 1:09meaning pandas. You'll likely need to
  31. 1:12download this package. And honestly, the
  32. 1:14easiest way to do that, if pandas is
  33. 1:16underlined red, you can just simply
  34. 1:19install this package. That should clear
  35. 1:21up that warning. Otherwise, what you
  36. 1:24could do is go to your left toolbar and
  37. 1:26open up a terminal. You can also use the
  38. 1:29shortcut of Alt + F12. You can use pip
  39. 1:32to install pandas.
  40. 1:37If you have multiple versions of Python
  41. 1:39downloaded, you'll need to use a
  42. 1:40different command. If that doesn't work,
  43. 1:43python-m
  44. 1:46then pip install pandas. One of those
  45. 1:49should work for you. Then hit enter.
  46. 1:54To check the current version of pandas,
  47. 1:56we can print
  48. 1:58access pandas as pd double underscore
  49. 2:02also known as dunder version double
  50. 2:06underscore again. And the current
  51. 2:08version of pandas that I'm running is
  52. 2:112.3.2.
  53. 2:13All right. And we are ready for our
  54. 2:15first lesson.
  55. 2:22All right, everybody. To kick off this
  56. 2:24Panda series, I got to talk about well,
  57. 2:26series. A series is a panda's
  58. 2:29one-dimensional labeled array. Think of
  59. 2:31it like a single column in a
  60. 2:33spreadsheet. It's one-dimensional. A
  61. 2:36good analogy would be a single hallway
  62. 2:38with apartments. Each apartment holds
  63. 2:41some data. Each apartment also has some
  64. 2:43sort of label or number. A series is a
  65. 2:46one-dimensional labeled array. Here's
  66. 2:48how we can create one. We'll start with
  67. 2:51a Python list. Let's say we have some
  68. 2:54data. Then we'll add a few numbers. 100
  69. 2:57102 104.
  70. 3:01I can convert this list to be a series.
  71. 3:04Here's how. I will create a series
  72. 3:06object equals access pandas spd call the
  73. 3:12series constructor.
  74. 3:14Now this is a constructor not a
  75. 3:16function. The s is uppercase.
  76. 3:19As an argument we will pass in our data.
  77. 3:23And then if I were to print my series
  78. 3:24object
  79. 3:26here's what it would say.
  80. 3:30There's a couple things going on here.
  81. 3:32All of our data is arranged in a single
  82. 3:35column. 100 102 104. The left hand side
  83. 3:39has an index. The index has labels
  84. 3:42starting with zero 0 1 2 and on the
  85. 3:46bottom we have metadata about this
  86. 3:48series. The data type of what we're
  87. 3:51storing are 64bit integers
  88. 3:54100 102 104. If we were to change the
  89. 3:57data type the metadata is going to
  90. 3:59change too. I'll add some decimal
  91. 4:02portions.1
  92. 4:042.3
  93. 4:07and let's see what changes.
  94. 4:10Now the data type is 64bit floatingoint
  95. 4:13numbers or we could use letters or
  96. 4:16strings
  97. 4:18A
  98. 4:21B
  99. 4:24C.
  100. 4:26Now the data type of what we're storing
  101. 4:28are objects
  102. 4:31or even booleans
  103. 4:33true
  104. 4:35false
  105. 4:37true.
  106. 4:40The data type of this series is bool
  107. 4:42meaning boolean true or false. But let's
  108. 4:46change these back to be numbers. 100 102
  109. 4:50104.
  110. 4:53Now when you construct a series object,
  111. 4:56you can set an index with custom labels.
  112. 4:59The default behavior for each label is
  113. 5:01to start at zero and increment by one.
  114. 5:04But we can pass in something custom.
  115. 5:05This will be another argument within our
  116. 5:07series constructor. We'll pass in a
  117. 5:09keyword argument for readability
  118. 5:12index equals. We can pass in a list, a
  119. 5:16tpple, a dictionary, a numpy array, or
  120. 5:19even another series. To keep it simple,
  121. 5:22let's set the first label to be A, then
  122. 5:25B, then C.
  123. 5:28Here we go. Here are the new labels.
  124. 5:31With our index, we have new labels, A B
  125. 5:35C, but the values are still the same,
  126. 5:37100, 102, 104. You could get creative
  127. 5:40with your labels. For example, going
  128. 5:42back to our apartment analogy, let's say
  129. 5:45apartment
  130. 5:46number one, then apartment number two,
  131. 5:52then apartment number three.
  132. 5:55We have apartment one is 100, apartment
  133. 5:58number two is 102, apartment number
  134. 6:01three is 104.
  135. 6:06We'll change that back. If at any time
  136. 6:09you need to access a value directly
  137. 6:10within a series, you'll take your
  138. 6:12series, access the lock property. Lock
  139. 6:16means location by label, but people just
  140. 6:19say lock.
  141. 6:21Use the subscript operator which is a
  142. 6:23set of straight brackets. Then you will
  143. 6:26select a label for example a. Return the
  144. 6:30value where the label is a.
  145. 6:34This will return a value whatever is at
  146. 6:36this label. A is 100,
  147. 6:40B is 102,
  148. 6:44C is 104. If there's a label that
  149. 6:46doesn't exist, like D, we get a key
  150. 6:49error. Let's change that back.
  151. 6:53To update one of these values, we'll
  152. 6:55have to access that lock property.
  153. 6:58Take our series,
  154. 7:00access the lock property within the
  155. 7:02subscript operator, locate the value at
  156. 7:06label C.
  157. 7:08Then let's set this to be 200.
  158. 7:12Then we will print our series.
  159. 7:17C is now 200. So to print or access a
  160. 7:20value, you access the lock property.
  161. 7:23We're locating a value by label. Now you
  162. 7:26can also locate by integer position.
  163. 7:30That's iO series dot the letter I lock.
  164. 7:36If I need the first value, the integer
  165. 7:38location is going to be zero.
  166. 7:41That returns 100,
  167. 7:441 is 102,
  168. 7:47and 2 is 104. It's pretty similar to
  169. 7:50accessing an array in other programming
  170. 7:52languages. So you got eyelock for
  171. 7:54integer location or lock location by
  172. 7:57label.
  173. 7:59Okay. Now, we're going to filter by
  174. 8:01value. We'll need a few more values.
  175. 8:04Let's say we have 200 and 202. We'll
  176. 8:08need a few more labels though.
  177. 8:10Currently, there's a mismatch. Value
  178. 8:12error. Length of values five does not
  179. 8:14match the length of index 3. We'll add a
  180. 8:17few more.
  181. 8:19We have a b c d e.
  182. 8:25There we go. Let's filter by values. We
  183. 8:28will return any values that are 200 or
  184. 8:31greater. Here's how.
  185. 8:33take our series, use the subscript
  186. 8:36operator, and here we'll write a
  187. 8:38condition. Return any values within our
  188. 8:42series that are greater than or equal to
  189. 8:46200.
  190. 8:48This will print
  191. 8:50the label of D has a value of 200. The
  192. 8:53label of E has a value of 2002.
  193. 8:57Let's switch this to be less than 200.
  194. 9:02A is 100, B is 102, C is 104.
  195. 9:07All right, here's a more complex
  196. 9:09example. Rather than using a Python list
  197. 9:11for this next example, we'll be using a
  198. 9:13dictionary.
  199. 9:17As you guys might know, I really like
  200. 9:19pizza. And well, I need to go on a diet
  201. 9:22because I've eaten too much pizza. I'm
  202. 9:24going to use a Python dictionary to
  203. 9:26record how many calories I've eaten per
  204. 9:28day. I'll create a dictionary. We'll
  205. 9:30name it calories. Calories equals a
  206. 9:34Python dictionary. A dictionary is made
  207. 9:36of key value pairs.
  208. 9:39For the key of day one,
  209. 9:44for the value within this key value
  210. 9:45pair, I'll set this to be 1,750,
  211. 9:49meaning 1,750 calories. I'm trying to
  212. 9:53eat less than 2,000 calories per day. If
  213. 9:55you would like, feel free to pick
  214. 9:56different numbers.
  215. 9:58For day two,
  216. 10:01I didn't stick to my diet. On day two, I
  217. 10:04had 2,100 calories.
  218. 10:07Then for day three,
  219. 10:12I had 1,700
  220. 10:15calories.
  221. 10:17Now to create a series, again, we will
  222. 10:19type series equals axis pandas as pd.
  223. 10:23call the series constructor, then pass
  224. 10:26in our dictionary.
  225. 10:30Since our dictionary is made of key
  226. 10:32value pairs, we'll use the keys as the
  227. 10:35labels.
  228. 10:37So, you don't need to pass in an index
  229. 10:40like this.
  230. 10:44If we were to print our series, here's
  231. 10:46the output.
  232. 10:49This single column represents the
  233. 10:51calories I've eaten each day. And on the
  234. 10:54left hand side is each label for this
  235. 10:56column. We have day 1, day 2, day 3.
  236. 11:01Let's access the lock property to see
  237. 11:03how many calories I've eaten on a
  238. 11:04certain day. Day 1,750.
  239. 11:10Day 2,100.
  240. 11:14Day 3 1,700.
  241. 11:17Now let's update day three. Perhaps I
  242. 11:19cheated on my diet and I had a cookie.
  243. 11:22Well, to update a value, we'll access
  244. 11:24our series, access the lock property.
  245. 11:27We're locating by label
  246. 11:30at the label of day three,
  247. 11:32we'll add 500 calories,
  248. 11:35we'll say plus equals 500. To add 500,
  249. 11:40now on day three, the total amount of
  250. 11:42calories I've eaten is 2,200.
  251. 11:46If I were to print my series,
  252. 11:50here's what my chart looks like. 1,750,
  253. 11:552,100, 2,200.
  254. 11:58Let's delete this line of code.
  255. 12:01Now, we'll filter by value. Let's find
  256. 12:04any days where I've eaten more than
  257. 12:062,000 calories. Which days have I not
  258. 12:09followed my diet plan? We'll access our
  259. 12:12series. Use the subscript operator.
  260. 12:15Return any values within our series
  261. 12:17that's greater than 2,00. We'll say
  262. 12:20greater than or equal to 2,00.
  263. 12:24All right. On day two, I did not stick
  264. 12:27to my diet. I had 2,100 calories.
  265. 12:31Now, on which days did I stick to my
  266. 12:33diet? Return any values within my series
  267. 12:37that are under 2,000.
  268. 12:41There are two days which I followed my
  269. 12:43diet where the calories is under 2,000.
  270. 12:46Day 1 and day three. All right
  271. 12:49everybody, that's a series. It's a
  272. 12:51pandas one-dimensional labeled array.
  273. 12:54Think of it like a single column in a
  274. 12:56spreadsheet. Now, your homework is to
  275. 12:58post a series in the comment section
  276. 13:00down below. If you can, try and think of
  277. 13:02a unique example. All right, everybody.
  278. 13:05And those are series using pandas.
  279. 13:14All right, let's do this everybody. We
  280. 13:16got to talk about data frames in pandas.
  281. 13:18A dataf frame is a tabular data
  282. 13:20structure with rows and columns. It's
  283. 13:23two-dimensional similar to an Excel
  284. 13:25spreadsheet. Here's a beginner friendly
  285. 13:28way in which we can create a data frame.
  286. 13:30Let's start with a dictionary. We'll say
  287. 13:32data equals a dictionary.
  288. 13:36For our data, we'll have some employees.
  289. 13:38For the key of name, this will be a
  290. 13:41column.
  291. 13:44Instead of just having a single value,
  292. 13:46we'll use a list, a Python list. Here,
  293. 13:48we can list a few different names. I'm
  294. 13:51going to use Spongebob characters
  295. 13:52because most people know who Spongebob
  296. 13:54is. We'll have Spongebob,
  297. 13:57Patrick,
  298. 14:00and Squidward.
  299. 14:03Then we'll add another column.
  300. 14:07This column will be for the age. How old
  301. 14:10is each employee? The value will be
  302. 14:12another Python list. I'm going to say
  303. 14:15that Spongebob is around 30. Patrick
  304. 14:18will be 35. Squidward will be 50. Let me
  305. 14:22just do a little bit of organizing.
  306. 14:25There. That looks pretty good. Now,
  307. 14:28we're going to convert our dictionary
  308. 14:30into a data frame. Here's how. We will
  309. 14:32assign dataf frame but most people
  310. 14:35shorten this to just df meaning dataf
  311. 14:38frame. Our dataf frame object equals
  312. 14:42access pandas spd. Call the data frame
  313. 14:46constructor.
  314. 14:48Now the d is capital not lowercase.
  315. 14:51That's because this is a constructor
  316. 14:53rather than a function.
  317. 14:55We're going to pass in our data, our
  318. 14:57dictionary.
  319. 15:00And then we will print our data frame
  320. 15:03which we have named DF for short.
  321. 15:09And here's our data frame. It's tabular
  322. 15:12data. It's made of rows
  323. 15:15and columns.
  324. 15:19We're also given an index. This index
  325. 15:21has a label for each row starting with
  326. 15:23zero. But you can set that to be
  327. 15:26something custom.
  328. 15:28To do so, when we construct a dataf
  329. 15:30frame object, we will set an index by
  330. 15:33passing in a keyword argument index
  331. 15:36equals a Python list. We'll say employee
  332. 15:401, then employee 2,
  333. 15:44then employee 3.
  334. 15:49And we should have some new labels.
  335. 15:51There we go. Employee one is Spongebob,
  336. 15:54age30. Employee 2 is Patrick, age35.
  337. 15:58Employee 3 is Squidward, age 50. If you
  338. 16:01need to select a single row, you can
  339. 16:04access the lock property. DF.Lock
  340. 16:07subscript operator. Lock means location
  341. 16:10by label.
  342. 16:12Give me all the data for employee one.
  343. 16:16That's the label.
  344. 16:19We are given Spongebob age 30. Here's
  345. 16:23the label and the data type of this row.
  346. 16:25It's an object. Let's select employee 2,
  347. 16:30Patrick, age 35. The label is employee
  348. 16:332. The data type for this row is an
  349. 16:36object.
  350. 16:37And employee 3,
  351. 16:40we have Squidward, age 50. Employee 3
  352. 16:43data type is an object. Now you can also
  353. 16:45select by integer location by using the
  354. 16:48eyelock property. I lock.
  355. 16:52So the index zero will give you the
  356. 16:54first row which is again Spongebob. One
  357. 16:59is for Patrick. Two is for Squidward.
  358. 17:06Let's just print the entire data frame.
  359. 17:08Now if you need to add a new column,
  360. 17:10here's how.
  361. 17:13I'll just add a comment.
  362. 17:15Add a new column.
  363. 17:20And I'm just going to zoom out a little
  364. 17:21bit. To create a new column, we'll
  365. 17:24access our data frame. Use the subscript
  366. 17:27operator. Then we'll need to think of a
  367. 17:29new column name. Let's say job or
  368. 17:32position. What does everybody do?
  369. 17:36We will assign this to be a Python list.
  370. 17:40The number of values must match the
  371. 17:42number of rows. We have three rows, one
  372. 17:44for Spongebob, Patrick, and Squidward.
  373. 17:47In that order, what is the job of
  374. 17:50Spongebob? He is a cook. That's the
  375. 17:52first value. Let's say Patrick is a new
  376. 17:55employee. He doesn't have a position
  377. 17:57quite yet. For the time being, we'll say
  378. 18:00NA for not available. And then Squidward
  379. 18:03is a cashier.
  380. 18:06Now, let's print our data frame. And we
  381. 18:08should have a new column.
  382. 18:10Employee one is a cook. Employee two,
  383. 18:13their job is not available yet. And
  384. 18:15employee three, employee 3 is a cashier.
  385. 18:19That's how to make a new column. Now,
  386. 18:21how do we add a new row? Here's how.
  387. 18:24Add a new row.
  388. 18:28An easy way to create a new row is to
  389. 18:30create a new data frame, then
  390. 18:32concatenate it. We'll say new
  391. 18:36row equals access pandas spd call the
  392. 18:41dataf frame constructor again.
  393. 18:44Now we're going to pass in a list. For
  394. 18:47this list, you can add a dictionary for
  395. 18:49each new row you want to add. For
  396. 18:51example, if I need to add three rows,
  397. 18:52I'll need three dictionaries within this
  398. 18:54list. But for now, we'll just add one.
  399. 18:58This dictionary needs key value pairs.
  400. 19:01For the key of name, we'll add, let's
  401. 19:05say, Sandy.
  402. 19:09For the key of age, we'll add a value of
  403. 19:1228.
  404. 19:15Then for the key of job,
  405. 19:18we'll add a value of I would say that
  406. 19:21SY's some sort of scientist or engineer,
  407. 19:23but we'll just say engineer.
  408. 19:26This is a separate data frame. It only
  409. 19:29contains a single row, a new record for
  410. 19:32Sandy. We're going to concatenate it to
  411. 19:34our existing data frame. We will
  412. 19:36reassign our data frame df equals
  413. 19:41access pandas spd. Call the concat
  414. 19:44function for concatenate. You may see
  415. 19:47people use append, but that's now
  416. 19:49depreciated. We'll be using concat for
  417. 19:51concatenation.
  418. 19:53We will pass in a Python list. Then
  419. 19:56include all of the data frames we want
  420. 19:58to concatenate. We want to include our
  421. 20:01existing data frame and the new row.
  422. 20:05And then let's print our data frame. And
  423. 20:07we should have a new row. Yes, we have a
  424. 20:10new row for Sandy.
  425. 20:12However, we have not set an index. The
  426. 20:15index was set to zero. Well, we can
  427. 20:17manually update that. When we create a
  428. 20:20new data frame, you can pass in an index
  429. 20:22as a keyword argument. We'll do that for
  430. 20:25Sandy.
  431. 20:26So, comma, space, and just for
  432. 20:29readability, I'll put this on a new
  433. 20:31line. Index equals a Python list with a
  434. 20:36new label for her of employee 4.
  435. 20:42And here we go. Employee 4 is Sandy, age
  436. 20:4528. Her job is an engineer.
  437. 20:50Now you can add more than one row. Let's
  438. 20:53add new rows.
  439. 20:56This variable will be new rows. So
  440. 20:59within our list, we can add more than
  441. 21:02one dictionary. Let's add another row.
  442. 21:05I'll do some copying and pasting just to
  443. 21:07save a little bit of time. Copy all the
  444. 21:10key value pairs and paste it within the
  445. 21:12new dictionary.
  446. 21:14Let's say the new employee is Mr. Krabs.
  447. 21:17His first name is Eugene. Eugene Krabs.
  448. 21:21He seems like he's about 60.
  449. 21:25His job is that he is a manager.
  450. 21:28We'll assign him to be employee 5.
  451. 21:35And then we are adding new rows to our
  452. 21:38data frame. All right, let's take a
  453. 21:40look.
  454. 21:43There they are. We have two new rows.
  455. 21:45one for Sandy and another for Eugene.
  456. 21:48Sandy is 28. She's an engineer. Eugene
  457. 21:51is 60. He's a manager. All right,
  458. 21:54everybody. Those are data frames. Dataf
  459. 21:56frames are a tabular data structure.
  460. 21:59They contain rows and columns. Your
  461. 22:02homework is in the comment section to
  462. 22:04post the output of a dataf frame. Try
  463. 22:07and think of a unique example if you
  464. 22:08can. And well everybody, those are dataf
  465. 22:11frames using pandas.
  466. 22:19Okay, let's do this everybody. So, in
  467. 22:21this video I'm going to show you how we
  468. 22:23can import CSV and JSON files using
  469. 22:26pandas. You will need some data to work
  470. 22:28with. For this demonstration on my
  471. 22:31desktop, I have two files. One is a CSV
  472. 22:34file, meaning commaepparated values, and
  473. 22:37the other is a JSON file.json.
  474. 22:41For this demonstration, you'll need some
  475. 22:43data to work with. I looked online and I
  476. 22:46did find a CSV file that contains the
  477. 22:48stats for the original 150 Pokémon. For
  478. 22:51this video series, I'll be using Pokémon
  479. 22:53because many of us have grown up with
  480. 22:55Pokémon. We can relate to it, but feel
  481. 22:57free to use any kind of data that you
  482. 22:59would like to work with. Just make sure
  483. 23:01that it's in a CSV format.
  484. 23:06So for my data for number one we have
  485. 23:09Bulbasaur all the way to number 150
  486. 23:12where we have Mewtwo.
  487. 23:14But again the data you want to use is up
  488. 23:16to you. If you would like a copy of this
  489. 23:18data I'll post it in the comment section
  490. 23:20on YouTube. You can just copy and paste
  491. 23:22it like so. Then to create a CSV file
  492. 23:27wherever you would like to place this
  493. 23:28file. Right click, go to new text
  494. 23:31document.
  495. 23:33Open it. Paste your CSV data.
  496. 23:37Save it. Then be sure you save it as a
  497. 23:39CSV file. For example, data.csv.
  498. 23:45Then once you have your CSV file for
  499. 23:48convenience, I'll move it to my Python
  500. 23:49project folder.
  501. 23:52My CSV file is right next to my main
  502. 23:54Python file. We're going to create a
  503. 23:57data frame using our CSV data. DF
  504. 24:00meaning dataf frame equals access pandas
  505. 24:03as pd
  506. 24:05call the read
  507. 24:08csv function and then we have to pass in
  508. 24:11either a relative or absolute file path.
  509. 24:14This file is right next to my main
  510. 24:16Python file. I just need the file name
  511. 24:18including the extension. In this case,
  512. 24:21it's data.
  513. 24:23CSV.
  514. 24:24Let's take a look at our data frame. I
  515. 24:26will print my data frame. And here's
  516. 24:29what we have.
  517. 24:32I have 150 rows and seven columns.
  518. 24:37The data we're outputting, it's
  519. 24:39truncated. If you print the data frame
  520. 24:41directly, you'll be given the first five
  521. 24:43rows and the last five rows. Here I have
  522. 24:48the Pokemon from Bulbasaur to
  523. 24:50Charmeleon, numbers 1 through 5, and the
  524. 24:53last five, Moltres to Mewtwo, numbers
  525. 24:56146 and 150. If you need to print all
  526. 25:00the data in your dataf frame, to print
  527. 25:03the entire data frame, all the rows and
  528. 25:05columns, and not a truncated version,
  529. 25:07following our data frame, we will call
  530. 25:09the two string function. Be careful when
  531. 25:13working with a very large file. You'll
  532. 25:15print everything.
  533. 25:17This file isn't that big. We only have
  534. 25:20150 rows. So now we display all the data
  535. 25:22for our Pokémon.
  536. 25:25A few of the columns include their
  537. 25:27number in the Pokédex, their name, their
  538. 25:30first type such as grass, fire, water,
  539. 25:33their second type if they have one.
  540. 25:35Bulbasaur, Ivysaur, Venusaur are also a
  541. 25:37poison type, but many of them are
  542. 25:39missing a second type such as
  543. 25:41Charmander. Charmander doesn't have a
  544. 25:43second type, so the value here is not a
  545. 25:45number. nan. There's a height in meters,
  546. 25:49a weight in kilograms, and a legendary
  547. 25:52status, which is either zero or one. If
  548. 25:55this Pokémon is not legendary, they have
  549. 25:57a legendary value of zero. If they are
  550. 25:59legendary, for example, Mewtwo,
  551. 26:03Mewtwo has a legendary status of one,
  552. 26:06meaning that there's only ever one
  553. 26:07Mewtwo.
  554. 26:09That's what it means by legendary.
  555. 26:12Now to read a JSON file. Here's a JSON
  556. 26:15file that contains the same data. JSON
  557. 26:18means JavaScript object notation. For
  558. 26:20entry number one, we have Bulbasaur,
  559. 26:23Ivysaur, Venusaur all the way till
  560. 26:25Mewtwo, which has a number of 150. For
  561. 26:29convenience, I'll place this JSON file
  562. 26:31within my project folder.
  563. 26:34And then to read a JSON file.
  564. 26:37To read a JSON file, we will call the
  565. 26:40read JSON function.
  566. 26:43And then pass in the name of that file,
  567. 26:45including the extension
  568. 26:47data.json.
  569. 26:51And here's what we have. We have our
  570. 26:53same data again. All the original 150
  571. 26:56Pokémon. All right, that's how we can
  572. 26:58read a file using pandas. I couldn't
  573. 27:01think of any homework for you to do. So,
  574. 27:03to increase engagement with this video,
  575. 27:05post the name of your favorite Pokémon
  576. 27:07down in the comment section. All right,
  577. 27:09everybody. And that is how to read both
  578. 27:10CSV and JSON files using pandas.
  579. 27:19Why? Hello again everybody. In this
  580. 27:21video, I'm going to show you different
  581. 27:22selection techniques using pandas. In
  582. 27:25the previous lesson, we've loaded a CSV
  583. 27:27file. Here's what my data frame looks
  584. 27:29like. My data frame contains the
  585. 27:31original 150 Pokémon all the way from
  586. 27:34Bulbasaur to Mewtwo. All right, so we
  587. 27:37have a data frame. To make a selection
  588. 27:39by column, I'll just add this as a
  589. 27:42comment. Selection by column.
  590. 27:46What we'll do is print access our data
  591. 27:49frame. Use the subscript operator. Then
  592. 27:52we will pass in a column name. We are
  593. 27:55going to pass in the name column. And
  594. 27:59here's what we have. As a single column,
  595. 28:02we have all the names. And this is
  596. 28:04actually a series. If you have a lot of
  597. 28:06data to print, the default behavior for
  598. 28:09pandas is to print the first five rows
  599. 28:12and the last five rows. What you see
  600. 28:14here is a truncated version. If you need
  601. 28:17to print the entire thing, follow this
  602. 28:19with the two string function. Do be
  603. 28:23careful if you have a lot of data you're
  604. 28:25working with.
  605. 28:27And here are the names of the original
  606. 28:29150 Pokémon.
  607. 28:32Let's say we need the height, the height
  608. 28:34column.
  609. 28:37Well, I'm going to do a search for
  610. 28:39height. I will place that within the
  611. 28:41subscript operator.
  612. 28:44And here's the height of every Pokemon
  613. 28:46in meters
  614. 28:48or the weight.
  615. 28:53We are searching for the column of
  616. 28:55weight.
  617. 28:58And here are all the weights.
  618. 29:01This one's heavy. Whatever it is, I
  619. 29:02think that's Venusaur. 100 kg. Now to
  620. 29:06select multiple columns, we are going to
  621. 29:09print access our data frame. Then within
  622. 29:11the subscript operator, we'll pass in a
  623. 29:14Python list. A list of all the columns
  624. 29:17we would like to output. Let's output
  625. 29:19the name column,
  626. 29:21the height column,
  627. 29:24and the weight.
  628. 29:33We have three columns, the name, the
  629. 29:36height, and the weight. Then to display
  630. 29:38all the data, all the rows, you can
  631. 29:41follow this with the two-string
  632. 29:43function. Two string
  633. 29:50And we get all the rows and these three
  634. 29:52specific columns name, height, weight.
  635. 29:56All right, that is selection by column.
  636. 30:02Now we'll make a selection by row or
  637. 30:06rows.
  638. 30:07Let's take a look at our data frame.
  639. 30:11I'm going to print my data frame.
  640. 30:14Each of these rows has a label. The
  641. 30:17default behavior for pandas is to give
  642. 30:19each row a number for the label starting
  643. 30:21with zero. They're in ascending order.
  644. 30:24Well, we can access the lock property,
  645. 30:27then pass in a label.
  646. 30:30So since I didn't set a label for each
  647. 30:31of these rows, zero would correspond
  648. 30:34with the first row where we have
  649. 30:35Bulbasaur
  650. 30:39one would be Ivysaur.
  651. 30:42But optionally what you could do,
  652. 30:45I can set one of the columns to serve as
  653. 30:48the index so that you can access one of
  654. 30:51the names by label using the lock
  655. 30:52property. You're probably not going to
  656. 30:54remember which Pokémon is 145, but
  657. 30:57you'll remember it by its name, so it's
  658. 31:00easier to look up. So, what you could do
  659. 31:02is that when we read our CSV file, we
  660. 31:05can pass in a second argument, a keyword
  661. 31:08argument, index
  662. 31:11meaning column. Index column equals
  663. 31:15name. Then, let's take a look at our
  664. 31:17data frame.
  665. 31:21Now, we're setting the label of each row
  666. 31:23to be the Pokémon's name. We can do a
  667. 31:26search by name pretty easily. So, to set
  668. 31:28that up, you just pass in a keyword
  669. 31:30argument of index column, index call for
  670. 31:33short, then set which column you would
  671. 31:35like to serve as the index.
  672. 31:38So, then when we access our data frame,
  673. 31:40we will access the lock property,
  674. 31:42meaning location by label. Then, let's
  675. 31:45do a search for Pikachu. We'll pass in a
  676. 31:47string of Pikachu.
  677. 31:50And here's all the data for Pikachu.
  678. 31:52Pikachu is number 25. Type one is
  679. 31:55electric. Pikachu doesn't have a second
  680. 31:58type. That's nan, not a number. The
  681. 32:00height of Pikachu is 0.4 m. Weight 6 kg.
  682. 32:04Pikachu is not a legendary Pokémon. Here
  683. 32:07you have the name and the data type of
  684. 32:09what Pikachu is. It's an object.
  685. 32:12All right, let's do a search for
  686. 32:15Charizard. Charizard.
  687. 32:19And here's Charizard. Charizard is
  688. 32:21number six. Type one is fire. Type two
  689. 32:23is flying. Height 1.7 m. Weight 90.5 kg.
  690. 32:28Not legendary. If you don't want all the
  691. 32:30data when displaying your row. When
  692. 32:33locating by label, we can pass in a
  693. 32:36second argument. We can pass in a Python
  694. 32:39list of all the columns we would like to
  695. 32:41select. So with Charizard, I would just
  696. 32:43like the height
  697. 32:48and the weight.
  698. 32:51So we will pass in a Python list as the
  699. 32:54second argument that contains the
  700. 32:55columns we would like to display.
  701. 32:58There we go. With Charizard, we only
  702. 33:01have the height and the weight, those
  703. 33:03two columns.
  704. 33:04Now, you also can select a range of rows
  705. 33:07as well. After the Pokemon's name,
  706. 33:09you're going to use the slice operator,
  707. 33:11which is a colon. Give me everything
  708. 33:14between Charizard and
  709. 33:17let's say Blastoise.
  710. 33:20Blastoise.
  711. 33:23There we go. We have Charizard,
  712. 33:25Squirtle, Warro, Blastoise.
  713. 33:29You can also use integer-based indexing
  714. 33:32if you so choose.
  715. 33:34We're going to print access our data
  716. 33:36frame access by integer location.
  717. 33:42Let's say you need the first 10. You
  718. 33:44would pass in those indices 0 colon 11.
  719. 33:49Remember that the second number is
  720. 33:50exclusive. You would then be given the
  721. 33:53first 10 rows
  722. 33:55between Bulbasaur and Metapod.
  723. 33:59Maybe you need every second row. Then
  724. 34:02add a step of two.
  725. 34:05Now we're given every second row. You
  726. 34:07can see that the number is going up by
  727. 34:09two. As a second argument, you can
  728. 34:12select the indices of which columns you
  729. 34:14would like. I would like the first three
  730. 34:17columns. 0 col 3.
  731. 34:21With the range of 10 indices that I've
  732. 34:23selected, I specified I would only like
  733. 34:25the first three columns. Besides our
  734. 34:28index with all of our unique labels, we
  735. 34:30have the first three columns, each
  736. 34:32Pokemon's number in the Pokédex, their
  737. 34:34first type, and their second type if
  738. 34:36they have one.
  739. 34:39All right, now we'll go over an
  740. 34:40exercise.
  741. 34:42We'll have a user type in the name of a
  742. 34:44Pokémon, and then do a search for it.
  743. 34:46We'll create a variable of Pokemon
  744. 34:49equals call the input function to ask
  745. 34:52for some user input. Enter a Pokemon
  746. 34:56name.
  747. 35:00Using this technique, if the Pokemon's
  748. 35:02name doesn't exist, it's going to cause
  749. 35:04a key error.
  750. 35:06Using a try block, we will try and print
  751. 35:11access our data frame. Access the
  752. 35:14location property.
  753. 35:16Then instead of placing a string here
  754. 35:18with a Pokemon's name, such as Pikachu,
  755. 35:20we'll place in the string variable of
  756. 35:22Pokemon,
  757. 35:24we'll do a search for whatever the user
  758. 35:26types in. Now, if that Pokemon doesn't
  759. 35:29exist,
  760. 35:31we will catch an exception if it
  761. 35:32happens.
  762. 35:34Except key error,
  763. 35:39where we will print, I'll use an F
  764. 35:41string. We'll say that Pokemon's name.
  765. 35:46Pokemon not found. And now we can do a
  766. 35:50search for a Pokemon. Let's test it.
  767. 35:53Enter a Pokemon name. I'll type in
  768. 35:55Pikachu. Make sure that the first letter
  769. 35:57is capital. And here's the stats for
  770. 35:59Pikachu.
  771. 36:01Let's do a search for Snorlax.
  772. 36:08And here's the stats for Snorlax.
  773. 36:11What if there's a Pokemon that doesn't
  774. 36:12exist, like whatever that is? Well, this
  775. 36:17Pokemon was not found.
  776. 36:20I couldn't think of any homework for you
  777. 36:21to do. So, let me know what your
  778. 36:23favorite non-coding hobby is. Post it
  779. 36:26down in the comment section. And that is
  780. 36:28selection using pandas.
  781. 36:36Hey, what's going on everybody? I'm
  782. 36:38going to explain filtering using pandas.
  783. 36:40If I could summarize what filtering is,
  784. 36:43it's basically keeping the rows that
  785. 36:45match a condition when working with the
  786. 36:47data frame. In the previous lessons,
  787. 36:50we've been working with the data frame
  788. 36:52that contains the original 150 Pokémon.
  789. 36:55Make sure that you have some data to
  790. 36:56work with before starting this lesson.
  791. 36:58Let's say I would like to create a new
  792. 37:00data frame and filter any Pokémon that
  793. 37:03have a height above 2 m. we would have
  794. 37:05to access this height column and return
  795. 37:08any rows where the height is 2 m or
  796. 37:10above. Here's how we can do that. Let's
  797. 37:14create a new data frame. We'll call it
  798. 37:16tall Pokémon equals. We will access our
  799. 37:20data frame. Use the subscript operator.
  800. 37:23Let me zoom in a little bit. We're going
  801. 37:26to return a series. Access our data
  802. 37:28frame. Use the subscript operator. We
  803. 37:32will select the column of height.
  804. 37:36Then we will write a condition. Return
  805. 37:39any rows where the height is greater
  806. 37:41than or equal to two.
  807. 37:46And then we will output our new data
  808. 37:48frame tall Pokémon. And here's what we
  809. 37:50have.
  810. 37:52We have filtered out any Pokémon that
  811. 37:55have less than 2 m for height. You can
  812. 37:57see that with the height column, all the
  813. 37:59heights are 2 m or above.
  814. 38:02So really with filtering, we're just
  815. 38:04keeping the rows that match condition.
  816. 38:06Let's apply another filter. Let's say
  817. 38:09heavy Pokemon.
  818. 38:14Heavy Pokémon equals access our data
  819. 38:17frame. Use the subscript operator. We
  820. 38:20will select the column of weight. Access
  821. 38:22our data frame. Subscript operator. We
  822. 38:26will select the column of weight.
  823. 38:30return any Pokemon that have a weight
  824. 38:32that's greater than let's say greater
  825. 38:34than 100
  826. 38:37and then we will print our heavy Pokémon
  827. 38:40that new data frame
  828. 38:42and here we go. We've returned all the
  829. 38:45rows where the weight is over 100.
  830. 38:51Now let's return any Pokémon that are
  831. 38:53legendary. If a Pokémon's not legendary,
  832. 38:56this value is zero. If it is legendary,
  833. 38:58it's one.
  834. 39:03We'll create a new data frame of
  835. 39:04legendary
  836. 39:07Pokemon
  837. 39:08equals access our data frame. Use the
  838. 39:11subscript operator. We will select the
  839. 39:14column of legendary.
  840. 39:19Now I set this equal to be one if it is
  841. 39:21legendary.
  842. 39:25We have four legendary Pokemon.
  843. 39:27Articuno, Zapdos, Moltrace, Mewtwo.
  844. 39:31Now, what you could do for readability,
  845. 39:33the boolean value of true also equals 1.
  846. 39:38That would work too. If your CSV data
  847. 39:41contains booleans, mine doesn't, you
  848. 39:44don't necessarily need double equals
  849. 39:46true. That's redundant. Since my
  850. 39:48original CSV data contains zeros and
  851. 39:50ones, I would need that double equals
  852. 39:55Let's find any water Pokemon.
  853. 39:58Water Pokemon equals access our data
  854. 40:02frame subscript operator.
  855. 40:05We will select the column of type one.
  856. 40:09If type one is equal a string of water,
  857. 40:15then we will return any water Pokémon.
  858. 40:17Now, this is only for type one.
  859. 40:20We have Squirtle all the way down to
  860. 40:23Vaporeon.
  861. 40:25But what if their second type is water
  862. 40:28and not their first type? Well, we could
  863. 40:30use the or logical operator.
  864. 40:32If you're going to use a logical
  865. 40:34operator, be sure to enclose your first
  866. 40:36condition within a set of parenthesis.
  867. 40:39With the or logical operator, this is a
  868. 40:42C style operator.
  869. 40:44With Python, it would be or.
  870. 40:47But we're going to be using the C style
  871. 40:49operator which is a vertical bar for
  872. 40:51ore.
  873. 40:53We'll write another condition. Access
  874. 40:55our data frame. Select the column of
  875. 40:58type two
  876. 41:00where the second type
  877. 41:02is equal to water.
  878. 41:06If the first type is water or the second
  879. 41:08type is water, return that row.
  880. 41:14See, we have more rows. There's a few
  881. 41:16Pokémon that have a first type that's
  882. 41:18not water, but their second type is
  883. 41:20water.
  884. 41:22Now, we'll use the and logical operator.
  885. 41:26Let's find any Pokémon that are fire and
  886. 41:29flying. We'll name this new data frame
  887. 41:31just FF for short. Fireflying.
  888. 41:34Fireflying Pokémon equals access our
  889. 41:38data frame. We'll do a search for any
  890. 41:40fire Pokémon. Select the column
  891. 41:44of type one.
  892. 41:46Check to see if that's equal to a string
  893. 41:49of fire. Let's see what we have
  894. 41:52currently.
  895. 41:54Here's all the fire Pokémon. Starting
  896. 41:56with Charmander all the way to Moltrace.
  897. 42:01Now, we'll see if their second type is
  898. 42:03flying. We'll be using a C style and
  899. 42:05logical operator, which is an amperand.
  900. 42:09Be sure you're not using the Python and.
  901. 42:12If we're using a logical operator,
  902. 42:14enclose your conditions within a set of
  903. 42:16parenthesis.
  904. 42:18Now, for my second condition, access our
  905. 42:21dataf frame subscript operator. Select
  906. 42:24the column of type two.
  907. 42:27Let's check to see if type two is equal
  908. 42:29to flying.
  909. 42:33Now, we're returning any fire and flying
  910. 42:35Pokémon. And there's two, Charizard and
  911. 42:39Moltrace. Their type one is fire. Their
  912. 42:42type two is flying.
  913. 42:45All right, everybody. That is filtering.
  914. 42:47Basically, you're keeping the rows that
  915. 42:49match a condition. For your homework, to
  916. 42:52help increase engagement with this
  917. 42:53video. In the comment section, post a
  918. 42:55data frame where you search for two
  919. 42:57different types. Try and pick some
  920. 42:59different types that we haven't used
  921. 43:00yet. And well, everybody, that is
  922. 43:02filtering using pandas.
  923. 43:10In this video, I'm going to explain
  924. 43:12aggregate functions using pandas.
  925. 43:14Aggregate functions. They reduce a set
  926. 43:17of values into a single summary value.
  927. 43:20Basically, we're reducing a bunch of
  928. 43:22values into one. We use aggregate
  929. 43:25functions to summarize and analyze data.
  930. 43:27In this demonstration, I have a data
  931. 43:29frame that contains the original 150
  932. 43:32Pokémon. Let's say for some of these
  933. 43:34columns such as the number, the height,
  934. 43:37the weight, and legendary status, I
  935. 43:39would like to find the mean or the
  936. 43:41average. To find the mean for all
  937. 43:43numeric columns, we can do the
  938. 43:45following.
  939. 43:46When we output our data frame, we will
  940. 43:48call the mean aggregate function. But
  941. 43:51there's one thing we're missing.
  942. 43:54We can't use the mean function on any
  943. 43:56columns that are non-numeric.
  944. 43:59We can't find the mean for anything
  945. 44:00that's non-numeric such as Pokémon
  946. 44:03names. They're alphabetical characters.
  947. 44:06What we could do is that when we call
  948. 44:08the mean function, we can pass in a
  949. 44:10keyword argument
  950. 44:12numeric_on
  951. 44:16equals true.
  952. 44:18We're telling pandas find the mean of
  953. 44:20any columns that are numeric. And here's
  954. 44:23what we have. The average number is
  955. 44:2675.5. The average height is 1.2 m. The
  956. 44:30average weight is 46.2 kg. The average
  957. 44:34amount of legendaries is 02 2%.
  958. 44:39So we have mean. Let's do some copying
  959. 44:42and pasting. Another aggregate function
  960. 44:46is sum. Let's sum all the numeric
  961. 44:49columns.
  962. 44:50And here's the sum. the sum of all the
  963. 44:52numbers, the sum of all the heights, the
  964. 44:55sum of all the weights, and the sum of
  965. 44:57the numbers of all the legendaries.
  966. 45:00Let's find the minimum.
  967. 45:03We will use the min aggregate function.
  968. 45:07The minimum number is 1. The minimum
  969. 45:10height is 2 m. The minimum weight is.1
  970. 45:13kg. And the minimum legendary status is
  971. 45:16zero. Then we have max.
  972. 45:21We will call the max aggregate function.
  973. 45:25Here are the max values. 150 8.8 460
  974. 45:301.
  975. 45:33We also have count.
  976. 45:38With the count function, we don't need
  977. 45:39to pass in a keyword argument. We are
  978. 45:42counting the number of values within
  979. 45:44each column. 150 150 150. Now, not every
  980. 45:48Pokémon has a type two. That's why this
  981. 45:50is only 67. Count won't include any null
  982. 45:53values. That's why this number is 67 and
  983. 45:56the rest is 150.
  984. 46:00That's using aggregate functions with an
  985. 46:02entire data frame. These aggregate
  986. 46:04functions would apply to the whole data
  987. 46:07frame. The next set of aggregate
  988. 46:10functions will be for a single column.
  989. 46:14All right, just to save a little bit of
  990. 46:15time, let's do some copying and pasting.
  991. 46:18We'll copy our five previous print
  992. 46:19statements and paste them.
  993. 46:22Now rather than selecting the entire
  994. 46:24data frame, we will select a single
  995. 46:26column by using the subscript operator.
  996. 46:30Then selecting a column, let's pick
  997. 46:32height. What is the average height?
  998. 46:35Since height is a numeric column, we
  999. 46:37don't have to pass in this keyword
  1000. 46:38argument. What is the mean of the height
  1001. 46:42column?
  1002. 46:43That is 1.2. 1.2 m.
  1003. 46:47Then let's do this with sum
  1004. 46:50subscript operator. Select the column of
  1005. 46:53height and we don't need to pass in that
  1006. 46:55keyword argument. What is the sum of all
  1007. 46:58the heights? 180 m.
  1008. 47:02Then let's do this with min. What is the
  1009. 47:04minimum value? What is the minimum
  1010. 47:06height in this column?
  1011. 47:11That is 0.2 m.
  1012. 47:15subscript operator we will select the
  1013. 47:17column of height and we don't need to
  1014. 47:19pass in this keyword argument with our
  1015. 47:22column of height the maximum value is
  1016. 47:248.8 8 m.
  1017. 47:27And what's the count?
  1018. 47:29Select our column of height
  1019. 47:33and the count
  1020. 47:35is 150. That's 150 non-null values.
  1021. 47:41But if I was to pick a different column
  1022. 47:42like type two, well, if we remember that
  1023. 47:45was only 67.
  1024. 47:48All right, that's how to use aggregate
  1025. 47:49functions with a single column. Select
  1026. 47:52your data frame. Then select the column
  1027. 47:54using the subscript operator.
  1028. 47:56Now there's also something called group
  1029. 47:59by. Group by is a function.
  1030. 48:02Now if I was to print my data frame, I
  1031. 48:05will print my data frame.
  1032. 48:08We can arrange our data frame into
  1033. 48:10different groups. We can group together
  1034. 48:12rows by something that they have in
  1035. 48:13common. Let's group our data frame by
  1036. 48:16each creature's type, their type one.
  1037. 48:18We'll put all the grass types in one
  1038. 48:20group, all the fire types in another
  1039. 48:23group, and we'll do this with all the
  1040. 48:24different types.
  1041. 48:28We will do this by creating a group by
  1042. 48:31object.
  1043. 48:32Group equals access our data frame. Call
  1044. 48:36the group by function. Then we're going
  1045. 48:40to pass in the name of a column by which
  1046. 48:42we want to group these records by.
  1047. 48:46We'll group by type one.
  1048. 48:48Now, if you were to output your group
  1049. 48:50directly,
  1050. 48:52here's what you would see.
  1051. 48:56Group is an object. When you apply an
  1052. 48:59aggregate function, the group object
  1053. 49:01will return a series for you to work
  1054. 49:03with and display. Let's take our group,
  1055. 49:06select the height column,
  1056. 49:09then call an aggregate function. What is
  1057. 49:11the average height for each group?
  1058. 49:16and we are grouping by type one.
  1059. 49:19Here's what we have. Here are the
  1060. 49:20average heights for bug types. The
  1061. 49:23average height is 0.9 m. Dragon types
  1062. 49:26have an average height of 2.6.
  1063. 49:29So, we are grouping by each Pokémon's
  1064. 49:31type. Their type one.
  1065. 49:36Let's find the sum.
  1066. 49:38We are grouping by type one. Let's find
  1067. 49:40the sum.
  1068. 49:44All of the bug types have a combined
  1069. 49:46height of 10.8 m. All the dragon types
  1070. 49:49have a combined height of 8 m.
  1071. 49:52Seems like water types have the greatest
  1072. 49:54combined height.
  1073. 49:58Let's find the minimum value from each
  1074. 50:00type.
  1075. 50:02We will call the min function.
  1076. 50:05And here's the minimum values for each
  1077. 50:07type or the max.
  1078. 50:11Here
  1079. 50:17are all the max values for each type
  1080. 50:22and the count.
  1081. 50:30Here's the total number of bug types,
  1082. 50:3212. Dragon types, three. Electric types,
  1083. 50:359. So on and so forth. There's a lot of
  1084. 50:38water types I noticed.
  1085. 50:41All right, everybody. So, those are
  1086. 50:43aggregate functions. They reduce a set
  1087. 50:46of values into a single summary value.
  1088. 50:49They're used to summarize and analyze
  1089. 50:51data. And well, everybody, those are
  1090. 50:53aggregate functions using pandas.
  1091. 51:00Guess what? It's me again. So, today I'm
  1092. 51:02going to explain data cleaning using
  1093. 51:04pandas. Data cleaning is the process of
  1094. 51:07fixing or removing incomplete, incorrect
  1095. 51:10or irrelevant data. Approximately 75% of
  1096. 51:14the work that is done with pandas is
  1097. 51:16specifically for data cleaning. You do a
  1098. 51:18lot of it. In this demonstration, I have
  1099. 51:20a data frame that contains the original
  1100. 51:23150 Pokémon. Now, let's say you would
  1101. 51:25like to drop a column because it's not
  1102. 51:27relevant. We're going to take our data
  1103. 51:30frame, call the drop function as a
  1104. 51:33keyword argument. We're going to pass in
  1105. 51:35columns equals. Then within a Python
  1106. 51:38list, you can list all the columns you
  1107. 51:40would like to drop. I would like to drop
  1108. 51:42that column of legendary.
  1109. 51:45This function of drop does return a new
  1110. 51:47data frame. So we're going to reassign
  1111. 51:49our current data frame. Dataf frame
  1112. 51:52equals then call the drop function of
  1113. 51:55the previous data frame. And let's take
  1114. 51:57a look. We should be missing that
  1115. 51:59column, which we are. Now, let's say you
  1116. 52:02don't want the number.
  1117. 52:04Well, we can add that to the Python
  1118. 52:05list. Drop. I think it was N O.
  1119. 52:11Yes, that column is also gone. That's
  1120. 52:15how to drop irrelevant columns.
  1121. 52:18Here's how to handle missing data in my
  1122. 52:21data frame. There are many Pokemon that
  1123. 52:24don't have a second type, but all
  1124. 52:26Pokemon do have a first type. For
  1125. 52:28example, Charmander. Charmander is a
  1126. 52:30fire type, but Charmander doesn't have a
  1127. 52:33second type. It's a pure fire type. If a
  1128. 52:37row is missing a value within type two,
  1129. 52:39we're going to drop that entire row.
  1130. 52:41Here's how to do that. We're going to
  1131. 52:44reassign our data frame,
  1132. 52:46access our data frame, call the drop na
  1133. 52:50method.
  1134. 52:53We're going to pass in a keyword
  1135. 52:54argument of subset equals a Python list.
  1136. 52:59Any columns we place within this list
  1137. 53:01will drop those rows if they're missing
  1138. 53:03a value.
  1139. 53:05Within type two, drop any rows that are
  1140. 53:08missing a value.
  1141. 53:11When I display my data frame, I'm going
  1142. 53:13to call the two string function just so
  1143. 53:14we can see the whole thing.
  1144. 53:17Two string function.
  1145. 53:22There should be no rows for Pokemon that
  1146. 53:24are missing type two. You can see that
  1147. 53:26they're numbered here. One, two, three.
  1148. 53:28We go to six because four through five
  1149. 53:31are missing. And then we jump up to 12
  1150. 53:33where we have Butterfree. We have
  1151. 53:35removed any rows where there are null
  1152. 53:37values within type two.
  1153. 53:41In place of dropping any rows where the
  1154. 53:44values are not available, let's replace
  1155. 53:46them instead with a different function.
  1156. 53:48We will reassign our data frame. dataf
  1157. 53:51frame equals access our dataf frame call
  1158. 53:54the fill na function fill any not
  1159. 53:59available values within this function
  1160. 54:02we're going to pass in a dictionary
  1161. 54:04within this dictionary we'll select a
  1162. 54:06column we'll select type two again
  1163. 54:11replace any not available values within
  1164. 54:14the column of type two with the
  1165. 54:16following value instead of not a number
  1166. 54:19nan and we'll replace those values with
  1167. 54:22none.
  1168. 54:24And let's take a look
  1169. 54:30for type two for any Pokémon that don't
  1170. 54:32have a second type. We've replaced any
  1171. 54:35missing values such as not a number with
  1172. 54:37the word none.
  1173. 54:40That's how to replace any missing
  1174. 54:42values.
  1175. 54:44Number three, we're going to fix any
  1176. 54:46inconsistent values.
  1177. 54:49We can replace any instance of a value
  1178. 54:52with another one within type one. Let's
  1179. 54:55select any grass types. We'll change any
  1180. 54:57instance of grass to be all uppercase
  1181. 54:59letters. Here's how you can do that.
  1182. 55:02We're going to take our data frame. Then
  1183. 55:04select a column. Within the column of
  1184. 55:07type one,
  1185. 55:09this will be a series that we're
  1186. 55:10selecting. We're going to access that
  1187. 55:14column using the subscript operator. We
  1188. 55:16are selecting the column of type one.
  1189. 55:20Call the replace function and then we
  1190. 55:23will pass in a dictionary.
  1191. 55:26Let's replace any instance of grass with
  1192. 55:30the following value. This is a
  1193. 55:32dictionary. We need a colon then space.
  1194. 55:35We'll replace grass with all capital
  1195. 55:38grass to test it. And that should
  1196. 55:41replace any instance of grass within
  1197. 55:43type one.
  1198. 55:45There we are.
  1199. 55:47Let's find a few more.
  1200. 55:51There's more grass types. Oddish, gloom,
  1201. 55:53vile plume.
  1202. 55:56This won't change any instance of grass
  1203. 55:58for type two because we've only selected
  1204. 56:00the type one column.
  1205. 56:02Let's change a few more. Within our
  1206. 56:04dictionary, we can pass in more than one
  1207. 56:06key value pair. Let's replace any
  1208. 56:08instance of fire with fire all caps.
  1209. 56:15Let's do the same with water.
  1210. 56:17Replace any instance of water within
  1211. 56:19type one with water all uppercase.
  1212. 56:25Yeah, it looks good. Grass is all
  1213. 56:27uppercase. Fire is all uppercase.
  1214. 56:31Water is all uppercase.
  1215. 56:34No other types are all uppercase.
  1216. 56:37They're capitalized normally.
  1217. 56:41Moving on, we can standardize text. Just
  1218. 56:45to demonstrate, within our name column,
  1219. 56:48let's take all the individual names and
  1220. 56:50make them lowercase. Here's how you can
  1221. 56:52do that.
  1222. 56:54We will select the column of name. Dataf
  1223. 56:57frame subscript operator. We are
  1224. 56:58selecting the column of name. This is a
  1225. 57:01series. Equals. Select that column
  1226. 57:04again. Dataf frame subscript operator.
  1227. 57:07We are selecting name. that series
  1228. 57:11access string call the lower function
  1229. 57:14and that's going to take our column of
  1230. 57:16name and make all of the characters
  1231. 57:19lowercase.
  1232. 57:21So that's how you can standardize some
  1233. 57:23text. Number five, I'm going to show you
  1234. 57:26how we can fix or change data types with
  1235. 57:29my column of legendary.
  1236. 57:31Non-leendary Pokémon have a value of
  1237. 57:33zero. Legendary Pokémon have a value of
  1238. 57:36one. I think booleans would make more
  1239. 57:39sense to use. We'll change these values
  1240. 57:41to be booleans. Here's how. We'll select
  1241. 57:44a column, access our data frame,
  1242. 57:47subscript operator, select the column of
  1243. 57:50legendary. This is a series. We're going
  1244. 57:53to reassign it. Access that column
  1245. 57:55again. Dataf frame subscript operator
  1246. 57:58legendary. And I'm just going to copy
  1247. 58:00this because I'm lazy. We'll call the as
  1248. 58:04type function. and then pass in a new
  1249. 58:06data type. For booleans, you type bool.
  1250. 58:10And now for the legendary status, it
  1251. 58:12should all be booleans.
  1252. 58:14Dratini, dragon, dragonite, they're not
  1253. 58:17legendary Pokémon. That's false. But
  1254. 58:19Mewtwo is a legendary Pokémon. That's
  1255. 58:22true. Same thing applies to Articuno,
  1256. 58:25Zapdos, Moltrace.
  1257. 58:29Here's how to remove duplicate values.
  1258. 58:32Going to our CSV data. Let's add a few
  1259. 58:35extra copies of Pokemon. Let's add a few
  1260. 58:37more copies of Bulbasaur.
  1261. 58:40I don't know, a total of four is good.
  1262. 58:43And then a couple for Mewtwo.
  1263. 58:48We have duplicate entries.
  1264. 58:52Let's print our data frame just to be
  1265. 58:54sure that we do have the duplicates.
  1266. 58:57Yeah, we got four entries for Bulbasaur.
  1267. 59:00And then at the bottom we have three
  1268. 59:03entries from U2.
  1269. 59:06To remove any duplicate values we will
  1270. 59:08reassign our data frame. Dataf frame
  1271. 59:10equals access our data frame. Call the
  1272. 59:13drop duplicates function.
  1273. 59:17Duplicates.
  1274. 59:19And that's all you got to do. And now we
  1275. 59:22should be missing those extra entries.
  1276. 59:24Yeah, there's only one Bulbasaur
  1277. 59:26and there's only one Mewtwo.
  1278. 59:29All right everybody. So that is data
  1279. 59:31cleaning. It's the process of fixing or
  1280. 59:33removing incomplete, incorrect or
  1281. 59:36irrelevant data. On average, 75% of the
  1282. 59:40work done in pandas is data cleaning.
  1283. 59:42It's an important topic. I couldn't
  1284. 59:44really think of any homework for you to
  1285. 59:46do. So to increase engagement with this
  1286. 59:48video, I don't know, post your favorite
  1287. 59:50anime in the comment section. And well
  1288. 59:52everybody, that is data cleaning using
  1289. 59:54pandas.

About this transcript

This page contains the full transcript of Learn Pandas in 1 hour! 🐼 by Bro Code, generated from the public captions YouTube serves with the video. The transcript has 8,014 words across 1,289 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.