基本运算

import pandas as pd
import numpy as np

算术运算

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

A B C D
0 0.760609 0.613241 0.364961 0.410891
1 0.595899 -0.036977 1.638757 0.324315
2 -0.449550 1.454056 -0.350593 -1.610116
3 -0.504186 -1.939660 -0.858069 -0.092820
4 0.460082 1.774454 2.345259 0.466701
5 0.773129 -0.156779 1.424565 -1.076731
6 0.861531 -0.962290 2.189390 -0.174458
7 -0.335252 -0.239888 -0.716997 -0.279536
8 0.123587 -0.166000 -0.722529 0.148577
9 -1.438719 -0.208563 -0.774170 2.349832
df = pd.DataFrame(np.random.randn(7, 3), columns=["A", "B", "C"])
df

A B C
0 0.989388 -0.268563 1.160158
1 -0.293817 2.287877 0.461996
2 0.359939 -0.985352 0.628079
3 -0.094576 -0.109829 -0.526178
4 0.462213 0.654518 1.245316
5 -0.222436 -1.475495 0.904898
6 0.060508 0.013139 1.111483
df * 5 + 2

A B C
0 6.946938 0.657185 7.800790
1 0.530917 13.439387 4.309981
2 3.799693 -2.926762 5.140393
3 1.527120 1.450855 -0.630888
4 4.311063 5.272590 8.226580
5 0.887822 -5.377473 6.524490
6 2.302542 2.065696 7.557416
1 / df

A B C
0 1.010726 -3.723522 0.861952
1 -3.403484 0.437086 2.164520
2 2.778252 -1.014865 1.592158
3 -10.573505 -9.105061 -1.900499
4 2.163507 1.527842 0.803009
5 -4.495684 -0.677739 1.105097
6 16.526648 76.107908 0.899699
df ** 4

A B C
0 0.958222 5.202168e-03 1.811626
1 0.007453 2.739876e+01 0.045557
2 0.016785 9.426843e-01 0.155617
3 0.000080 1.455019e-04 0.076653
4 0.045642 1.835213e-01 2.405018
5 0.002448 4.739696e+00 0.670500
6 0.000013 2.980443e-08 1.526201

布尔运算

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

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

a b
0 False True
1 True True
2 True False
df & df

a b
0 False True
1 True True
2 True False
df | df

a b
0 False True
1 True True
2 True False
df ^ df

a b
0 False False
1 False False
2 False False
-df

a b
0 True False
1 False False
2 False True
Previous
Next