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