Pandas

linkaiyi
Follow

dateframe main concept

Series

One row can be Series, in form of dict or list, it can be multi dimensions as well.

Axis

axis = 0 column wise
axis = 1 row wise

It specifies the axis along which the means are computed. By default axis=0. This is consistent with the numpy.mean usage when axis is specified explicitly (in numpy.mean, axis==None by default, which computes the mean value over the flattened array) , in which axis=0 along the rows (namely, index in pandas), and axis=1 along the columns. For added clarity, one may choose to specify axis=’index’ (instead of axis=0) or axis=’columns’ (instead of axis=1).

+——————+————-+————+
| | A | B |
+——————+————-+————-
| 0 | 0.626386| 1.52325|——axis=1——->
+——————+————-+————+
| |
| axis=0 |
↓ ↓

index

pandas will need index and use index as indictors

fruits = ['apples', 'oranges', 'cherries', 'pears']
fruits_tr = ['elma', 'portakal', 'kiraz', 'armut']
S = pd.Series([20, 33, 52, 10], index=fruits)
S2 = pd.Series([17, 13, 31, 32], index=fruits_tr)
print(S + S2)

---- # + only add values with same index, other index will have NaN

apples     NaN
armut      NaN
cherries   NaN
elma       NaN
kiraz      NaN
oranges    NaN
pears      NaN
portakal   NaN
dtype: float64

pandas operation apply to all values

import numpy as np
print((S + 3) * 4)
print("======================")
print(np.sin(S))
apples       92
oranges     144
cherries    220
pears        52
dtype: int64

pandas.Series.apply

Series.apply(func, convert_dtype=True, args=(), **kwds)

Die Funktion “func” wird auf das Series-Objekt angwendet und liefert, in Abhängigkeit von “func”, entweder ein Series-Objekt oder ein DataFrame-Objekt zurück.

Parameter Bedeutung
func Eine Funktion, die auf das gesamte Series-Objekt (Numpy-Funktion) oder nur auf einzelne Werte des Series (Python-Funktion) angewendet wird.
convert_dtype Ein Boolescher Wert. Wenn dieser auf True gesetzt wird (Standard), so wird versucht, bei der Anwendung einen besseren dtype für die elementweisen Funktions-Ergebnisse zu finden. Wenn der Parameter auf False gesetzt wird, so wird dtype=objekt verwendet.
args Positions-Argumente, die an die Funktion “func” übergeben werden, zusätzlich zu den Werten des Series-Objektes.
**kwds Zusätzliche Schlüsselwort-Argumente, die als Schlüsselworte an die Funktion übergeben werden.

Numpy np.NaN is None

method: Series.isnull(), Series.notnull(), Series.dropna(), Series.fillna()

Dataframe

Data frame is actually a combination of two Series, row and column, each has own index. Data frame can be created by concat several Series (axis important) or by dictionary with value as list, key as column name

Object has 0 attachments

Was this article helpful?

This article is viewed 14 times!

0 Comments

Leave a Comment

Support