|
| 1 | +.. currentmodule:: pandas |
| 2 | +.. _compare_with_excel: |
| 3 | + |
| 4 | +.. ipython:: python |
| 5 | + :suppress: |
| 6 | +
|
| 7 | + import pandas as pd |
| 8 | + import random |
| 9 | + pd.options.display.max_rows=15 |
| 10 | +
|
| 11 | +Comparison with Excel |
| 12 | +********************* |
| 13 | + |
| 14 | +Commonly used Excel functionalities |
| 15 | +----------------------------------- |
| 16 | + |
| 17 | +Fill Handle |
| 18 | +~~~~~~~~~~~ |
| 19 | + |
| 20 | +Create a series of numbers following a set pattern in a certain set of cells. In |
| 21 | +Excel this would be done by shift+drag after entering the first number or by |
| 22 | +entering the first two or three values and then dragging. |
| 23 | + |
| 24 | +This can be achieved by creating a series and assigning it to the desired cells. |
| 25 | + |
| 26 | +.. ipython:: python |
| 27 | +
|
| 28 | + df = pd.DataFrame({'AAA': [1] * 8, 'BBB': list(range(0, 8))}); df |
| 29 | +
|
| 30 | + series = list(range(1, 5)); series |
| 31 | +
|
| 32 | + df.iloc[2:(5+1)].AAA = series |
| 33 | +
|
| 34 | + df |
| 35 | +
|
| 36 | +Filters |
| 37 | +~~~~~~~ |
| 38 | + |
| 39 | +Filters can be achieved by using slicing. |
| 40 | + |
| 41 | +The examples filter by 0 on column AAA, and also show how to filter by multiple |
| 42 | +values. |
| 43 | + |
| 44 | +.. ipython:: python |
| 45 | +
|
| 46 | + df[df.AAA == 0] |
| 47 | +
|
| 48 | + df[(df.AAA == 0) | (df.AAA == 2)] |
| 49 | +
|
| 50 | +
|
| 51 | +Drop Duplicates |
| 52 | +~~~~~~~~~~~~~~~ |
| 53 | + |
| 54 | +Another commonly used function is Drop Duplicates. This is directly supported in |
| 55 | +pandas. |
| 56 | + |
| 57 | +.. ipython:: python |
| 58 | +
|
| 59 | + df = pd.DataFrame({"class": ['A', 'A', 'A', 'B', 'C', 'D'], "student_count": [42, 35, 42, 50, 47, 45], "all_pass": ["Yes", "Yes", "Yes", "No", "No", "Yes"]}) |
| 60 | +
|
| 61 | + df.drop_duplicates() |
| 62 | +
|
| 63 | + df.drop_duplicates(["class", "student_count"]) |
| 64 | +
|
| 65 | +
|
| 66 | +Pivot Table |
| 67 | +~~~~~~~~~~~ |
| 68 | + |
| 69 | +This can be achieved by using ``pandas.pivot_table`` for examples and reference, |
| 70 | +please see `pandas.pivot_table <http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html>`__ |
| 71 | + |
| 72 | + |
| 73 | +Formulae |
| 74 | +~~~~~~~~ |
| 75 | + |
| 76 | +Let's create a new column "girls_count" and try to compute the number of boys in |
| 77 | +each class. |
| 78 | + |
| 79 | +.. ipython:: python |
| 80 | +
|
| 81 | + df["girls_count"] = [21, 12, 21, 31, 23, 17]; df |
| 82 | +
|
| 83 | + def get_count(row): |
| 84 | + return row["student_count"] - row["girls_count"] |
| 85 | +
|
| 86 | + df["boys_count"] = df.apply(get_count, axis = 1); df |
| 87 | +
|
| 88 | +
|
| 89 | +VLOOKUP |
| 90 | +~~~~~~~ |
| 91 | + |
| 92 | +.. ipython:: python |
| 93 | +
|
| 94 | + df1 = pd.DataFrame({"keys": [1, 2, 3, 4, 5, 6, 7], "first_names": ["harry", "ron", |
| 95 | + "hermione", "rubius", "albus", "severus", "luna"]}); df1 |
| 96 | +
|
| 97 | + random_names = pd.DataFrame({"surnames": ["hadrid", "malfoy", "lovegood", |
| 98 | + "dumbledore", "grindelwald", "granger", "weasly", "riddle", "longbottom", |
| 99 | + "snape"], "keys": [ random.randint(1,7) for x in range(0,10) ]}) |
| 100 | +
|
| 101 | + random_names |
| 102 | +
|
| 103 | + random_names.merge(df1, on="keys", how='left') |
| 104 | +
|
| 105 | +Adding a row |
| 106 | +~~~~~~~~~~~~ |
| 107 | + |
| 108 | +To appended a row, we can just assign values to an index using ``iloc``. |
| 109 | + |
| 110 | +NOTE: If the index already exists, the values in that index will be over written. |
| 111 | + |
| 112 | +.. ipython:: python |
| 113 | +
|
| 114 | + df1.iloc[7] = [8, "tonks"]; df1 |
| 115 | +
|
| 116 | +
|
| 117 | +Search and Replace |
| 118 | +~~~~~~~~~~~~~~~~~~ |
| 119 | + |
| 120 | +The ``replace`` method that comes associated with the ``DataFrame`` object can perform |
| 121 | +this function. Please see `pandas.DataFrame.replace <https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html>`__ for examples. |
0 commit comments