@@ -19,7 +19,7 @@ Customarily, we import as follows:
19
19
Object creation
20
20
---------------
21
21
22
- See the :ref: `Data Structure Intro section <dsintro >`.
22
+ See the :ref: `Intro to data structures section <dsintro >`.
23
23
24
24
Creating a :class: `Series ` by passing a list of values, letting pandas create
25
25
a default integer index:
@@ -39,7 +39,8 @@ and labeled columns:
39
39
df = pd.DataFrame(np.random.randn(6 , 4 ), index = dates, columns = list (" ABCD" ))
40
40
df
41
41
42
- Creating a :class: `DataFrame ` by passing a dict of objects that can be converted to series-like.
42
+ Creating a :class: `DataFrame ` by passing a dictionary of objects that can be
43
+ converted into a series-like structure:
43
44
44
45
.. ipython :: python
45
46
@@ -56,7 +57,7 @@ Creating a :class:`DataFrame` by passing a dict of objects that can be converted
56
57
df2
57
58
58
59
The columns of the resulting :class: `DataFrame ` have different
59
- :ref: `dtypes <basics.dtypes >`.
60
+ :ref: `dtypes <basics.dtypes >`:
60
61
61
62
.. ipython :: python
62
63
@@ -116,14 +117,14 @@ of the dtypes in the DataFrame. This may end up being ``object``, which requires
116
117
casting every value to a Python object.
117
118
118
119
For ``df ``, our :class: `DataFrame ` of all floating-point values,
119
- :meth: `DataFrame.to_numpy ` is fast and doesn't require copying data.
120
+ :meth: `DataFrame.to_numpy ` is fast and doesn't require copying data:
120
121
121
122
.. ipython :: python
122
123
123
124
df.to_numpy()
124
125
125
126
For ``df2 ``, the :class: `DataFrame ` with multiple dtypes,
126
- :meth: `DataFrame.to_numpy ` is relatively expensive.
127
+ :meth: `DataFrame.to_numpy ` is relatively expensive:
127
128
128
129
.. ipython :: python
129
130
@@ -180,7 +181,7 @@ equivalent to ``df.A``:
180
181
181
182
df[" A" ]
182
183
183
- Selecting via ``[] ``, which slices the rows.
184
+ Selecting via ``[] ``, which slices the rows:
184
185
185
186
.. ipython :: python
186
187
@@ -278,13 +279,13 @@ For getting fast access to a scalar (equivalent to the prior method):
278
279
Boolean indexing
279
280
~~~~~~~~~~~~~~~~
280
281
281
- Using a single column's values to select data.
282
+ Using a single column's values to select data:
282
283
283
284
.. ipython :: python
284
285
285
286
df[df[" A" ] > 0 ]
286
287
287
- Selecting values from a DataFrame where a boolean condition is met.
288
+ Selecting values from a DataFrame where a boolean condition is met:
288
289
289
290
.. ipython :: python
290
291
@@ -303,7 +304,7 @@ Setting
303
304
~~~~~~~
304
305
305
306
Setting a new column automatically aligns the data
306
- by the indexes.
307
+ by the indexes:
307
308
308
309
.. ipython :: python
309
310
@@ -329,13 +330,13 @@ Setting by assigning with a NumPy array:
329
330
330
331
df.loc[:, " D" ] = np.array([5 ] * len (df))
331
332
332
- The result of the prior setting operations.
333
+ The result of the prior setting operations:
333
334
334
335
.. ipython :: python
335
336
336
337
df
337
338
338
- A ``where `` operation with setting.
339
+ A ``where `` operation with setting:
339
340
340
341
.. ipython :: python
341
342
@@ -352,27 +353,27 @@ default not included in computations. See the :ref:`Missing Data section
352
353
<missing_data>`.
353
354
354
355
Reindexing allows you to change/add/delete the index on a specified axis. This
355
- returns a copy of the data.
356
+ returns a copy of the data:
356
357
357
358
.. ipython :: python
358
359
359
360
df1 = df.reindex(index = dates[0 :4 ], columns = list (df.columns) + [" E" ])
360
361
df1.loc[dates[0 ] : dates[1 ], " E" ] = 1
361
362
df1
362
363
363
- To drop any rows that have missing data.
364
+ To drop any rows that have missing data:
364
365
365
366
.. ipython :: python
366
367
367
368
df1.dropna(how = " any" )
368
369
369
- Filling missing data.
370
+ Filling missing data:
370
371
371
372
.. ipython :: python
372
373
373
374
df1.fillna(value = 5 )
374
375
375
- To get the boolean mask where values are ``nan ``.
376
+ To get the boolean mask where values are ``nan ``:
376
377
377
378
.. ipython :: python
378
379
@@ -402,7 +403,7 @@ Same operation on the other axis:
402
403
df.mean(1 )
403
404
404
405
Operating with objects that have different dimensionality and need alignment.
405
- In addition, pandas automatically broadcasts along the specified dimension.
406
+ In addition, pandas automatically broadcasts along the specified dimension:
406
407
407
408
.. ipython :: python
408
409
@@ -527,14 +528,14 @@ See the :ref:`Grouping section <groupby>`.
527
528
df
528
529
529
530
Grouping and then applying the :meth: `~pandas.core.groupby.GroupBy.sum ` function to the resulting
530
- groups.
531
+ groups:
531
532
532
533
.. ipython :: python
533
534
534
535
df.groupby(" A" ).sum()
535
536
536
537
Grouping by multiple columns forms a hierarchical index, and again we can
537
- apply the :meth: `~pandas.core.groupby.GroupBy.sum ` function.
538
+ apply the :meth: `~pandas.core.groupby.GroupBy.sum ` function:
538
539
539
540
.. ipython :: python
540
541
@@ -565,7 +566,7 @@ Stack
565
566
df2
566
567
567
568
The :meth: `~DataFrame.stack ` method "compresses" a level in the DataFrame's
568
- columns.
569
+ columns:
569
570
570
571
.. ipython :: python
571
572
@@ -673,21 +674,21 @@ pandas can include categorical data in a :class:`DataFrame`. For full docs, see
673
674
674
675
675
676
676
- Convert the raw grades to a categorical data type.
677
+ Converting the raw grades to a categorical data type:
677
678
678
679
.. ipython :: python
679
680
680
681
df[" grade" ] = df[" raw_grade" ].astype(" category" )
681
682
df[" grade" ]
682
683
683
684
Rename the categories to more meaningful names (assigning to
684
- :meth: `Series.cat.categories ` is in place!).
685
+ :meth: `Series.cat.categories ` is in place!):
685
686
686
687
.. ipython :: python
687
688
688
689
df[" grade" ].cat.categories = [" very good" , " good" , " very bad" ]
689
690
690
- Reorder the categories and simultaneously add the missing categories (methods under :meth: `Series.cat ` return a new :class: `Series ` by default).
691
+ Reorder the categories and simultaneously add the missing categories (methods under :meth: `Series.cat ` return a new :class: `Series ` by default):
691
692
692
693
.. ipython :: python
693
694
@@ -696,13 +697,13 @@ Reorder the categories and simultaneously add the missing categories (methods un
696
697
)
697
698
df[" grade" ]
698
699
699
- Sorting is per order in the categories, not lexical order.
700
+ Sorting is per order in the categories, not lexical order:
700
701
701
702
.. ipython :: python
702
703
703
704
df.sort_values(by = " grade" )
704
705
705
- Grouping by a categorical column also shows empty categories.
706
+ Grouping by a categorical column also shows empty categories:
706
707
707
708
.. ipython :: python
708
709
@@ -722,7 +723,7 @@ We use the standard convention for referencing the matplotlib API:
722
723
723
724
plt.close(" all" )
724
725
725
- The :meth: `~plt.close ` method is used to `close <https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.close.html >`__ a figure window.
726
+ The :meth: `~plt.close ` method is used to `close <https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.close.html >`__ a figure window:
726
727
727
728
.. ipython :: python
728
729
@@ -754,13 +755,13 @@ Getting data in/out
754
755
CSV
755
756
~~~
756
757
757
- :ref: `Writing to a csv file. <io.store_in_csv >`
758
+ :ref: `Writing to a csv file: <io.store_in_csv >`
758
759
759
760
.. ipython :: python
760
761
761
762
df.to_csv(" foo.csv" )
762
763
763
- :ref: `Reading from a csv file. <io.read_csv_table >`
764
+ :ref: `Reading from a csv file: <io.read_csv_table >`
764
765
765
766
.. ipython :: python
766
767
@@ -778,13 +779,13 @@ HDF5
778
779
779
780
Reading and writing to :ref: `HDFStores <io.hdf5 >`.
780
781
781
- Writing to a HDF5 Store.
782
+ Writing to a HDF5 Store:
782
783
783
784
.. ipython :: python
784
785
785
786
df.to_hdf(" foo.h5" , " df" )
786
787
787
- Reading from a HDF5 Store.
788
+ Reading from a HDF5 Store:
788
789
789
790
.. ipython :: python
790
791
@@ -800,13 +801,13 @@ Excel
800
801
801
802
Reading and writing to :ref: `MS Excel <io.excel >`.
802
803
803
- Writing to an excel file.
804
+ Writing to an excel file:
804
805
805
806
.. ipython :: python
806
807
807
808
df.to_excel(" foo.xlsx" , sheet_name = " Sheet1" )
808
809
809
- Reading from an excel file.
810
+ Reading from an excel file:
810
811
811
812
.. ipython :: python
812
813
0 commit comments