基本运算

import pandas as pd
import numpy as np

算术运算

df = pd.DataFrame(np.random.randn(10, 4), columns=["A", "B", "C", "D"])
df

ABCD
00.7606090.6132410.3649610.410891
10.595899-0.0369771.6387570.324315
2-0.4495501.454056-0.350593-1.610116
3-0.504186-1.939660-0.858069-0.092820
40.4600821.7744542.3452590.466701
50.773129-0.1567791.424565-1.076731
60.861531-0.9622902.189390-0.174458
7-0.335252-0.239888-0.716997-0.279536
80.123587-0.166000-0.7225290.148577
9-1.438719-0.208563-0.7741702.349832
df = pd.DataFrame(np.random.randn(7, 3), columns=["A", "B", "C"])
df

ABC
00.989388-0.2685631.160158
1-0.2938172.2878770.461996
20.359939-0.9853520.628079
3-0.094576-0.109829-0.526178
40.4622130.6545181.245316
5-0.222436-1.4754950.904898
60.0605080.0131391.111483
df * 5 + 2

ABC
06.9469380.6571857.800790
10.53091713.4393874.309981
23.799693-2.9267625.140393
31.5271201.450855-0.630888
44.3110635.2725908.226580
50.887822-5.3774736.524490
62.3025422.0656967.557416
1 / df

ABC
01.010726-3.7235220.861952
1-3.4034840.4370862.164520
22.778252-1.0148651.592158
3-10.573505-9.105061-1.900499
42.1635071.5278420.803009
5-4.495684-0.6777391.105097
616.52664876.1079080.899699
df ** 4

ABC
00.9582225.202168e-031.811626
10.0074532.739876e+010.045557
20.0167859.426843e-010.155617
30.0000801.455019e-040.076653
40.0456421.835213e-012.405018
50.0024484.739696e+000.670500
60.0000132.980443e-081.526201

布尔运算

df = pd.DataFrame({"a": [1, 0, 1], "b": [0, 1, 1]}, dtype=bool)
df

ab
0TrueFalse
1FalseTrue
2TrueTrue
df = pd.DataFrame({"a": [0, 1, 1], "b": [1, 1, 0]}, dtype=bool)
df

ab
0FalseTrue
1TrueTrue
2TrueFalse
df & df

ab
0FalseTrue
1TrueTrue
2TrueFalse
df | df

ab
0FalseTrue
1TrueTrue
2TrueFalse
df ^ df

ab
0FalseFalse
1FalseFalse
2FalseFalse
-df

ab
0TrueFalse
1FalseFalse
2FalseTrue
Previous
Next