Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Functional Application of pandas

Shulou Source: shulou.com Published: 2022-06-03 07:02:51 11月23日 Update

The function of Pandas applies apply and applymap1. Functions that can be used directly with NumPy

Sample code:

Df = pd.DataFrame (np.random.randn (5,4)-1) print (df) print (np.abs (df))

Running result:

0 12 30-0.062413 0.844813-1.853721-1.9807171-0.539628-1.975173-0.856597-2.6124062-1.277081-1.088457-0.152189 0.5303253-1.356578-1.996441 0.368822-2.2114784-0.562777 0.518648-2.007223 0.059411 01230 0.062413 0.844813 1.853721 1.9807171 0.539628 1.975173 0.856597 2.6124062 1.277081 1.088457 0.152189 0.5303253 1.356578 1.996441 0.368822 2.2114784 0.562777 0.518648 2.007223 0.0594112. Apply a function to a column or row through apply

Sample code:

# apply row or column data using apply # f = lambda x: x.max () print (df.apply (lambda x: x.max ()

Running result:

0-0.0624131 0.8448132 0.3688223 0.530325dtype: float64 pay attention to the direction of the specified axis. Default axis=0, direction is column # specify axis direction, axis=1, direction is row print (lambda x: x.max (), axis=1) ```python0-0.0624131 0.8448132 0.3688223 0.530325dtype: float643. Apply a function to each data through applymap

Sample code:

# using applymap to apply to each data f2 = lambda x: '.2f'% xprint (df.applymap (f2))

Running result:

01 230-0.06 0.84-1.85-1.981-0.54-1.98-0.86-2.612-1.28-1.09-0.15 0.533-1.36-2.00 0.37-2.214-0.56 0.52-2.01 0.06 sort 1. Index sort sort_index ()

Sort uses ascending sort by default, and ascending=False uses descending sort.

Sample code:

# Seriess4 = pd.Series (range (10,15), index = np.random.randint (5, size=5)) print (S4) # Index sort s4.sort_index () # 0 0 1 3 3

Running result:

0 103 111 123 130 14dtype: int640 100 141 123 113 13dtype: int64 pay attention to the axis direction when operating on DataFrame

Sample code:

# DataFramedf4 = pd.DataFrame (np.random.randn (3,5), index=np.random.randint (3, size=3), columns=np.random.randint (5, size=5) print (df4) df4_isort = df4.sort_index (axis=1, ascending=False) print (df4_isort) # 4 2 1 1 0

Running result:

14 0 1 22-0.416686-0.161256 0.088802-0.004294 1.1641381-0.671914 0.531256 0.303222-0.509493-0.3425731 1.988321-0.466987 2.787891-1.105912 0.889082 1 02-0.161256 1.164138-0.416686-0.004294 0.0888021 0.531256 -0.342573-0.671914-0.509493 0.3032221-0.466987 0.889082 1.988321-1.105912 2.7878912. Sort sort_values by value (by='column name')

Sort by a unique column name, and an error is reported if there are other identical column names.

Sample code:

# sort df4_vsort = df4.sort_values (by=0, ascending=False) print (df4_vsort) by value

Running result:

14 0 1 21 1.988321-0.466987 2.787891-1.105912 0.8890821-0.671914 0.531256 0.303222-0.509493-0.3425732-0.416686-0.161256 0.088802-0.004294 1.164138 deal with missing data

Sample code:

Df_data = pd.DataFrame ([np.random.randn (3), [1, 2, np.nan], [np.nan, 4, np.nan], [1, 2, 3.]) print (df_data.head ())

Running result:

01 20-0.281885-0.786572 0.4871261 1.000000 2.000000 NaN2 NaN 4.000000 NaN3 1.000000 2.000000 3.0000001. Determine if there is a missing value: isnull ()

Sample code:

# isnullprint (df_data.isnull ())

Running result:

0 1 20 False False False1 False False True2 True False True3 False False False2. Discard missing data: dropna () discards rows or columns containing NaN based on the direction of the axis axis. Sample code: # dropnaprint (df_data.dropna ()) print (df_data.dropna (axis=1))

Running result:

0 1 20-0.281885-0.786572 0.4871263 1.000000 2.000000 3.000000 10-0.7865721 2.0000002 4.0000003 2.0000003. Populate missing data: fillna ()

Sample code:

# fillnaprint (df_data.fillna (- 100.)

Running result:

0 1 20-0.281885-0.786572 0.4871261 1.000000 2.000000-100.0000002-100.000000 4.000000-100.0000003 1.000000 2.000000 3.000000

Tags: Code example result run sort data direction application function missing index same ascending processing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Huawei macOS OPPO Reno Docker