This article is participating in Python Theme Month. See [activities]

The table used in this article is as follows:

Let’s look at the original situation:

Import pandas as pd df = pd.read_excel(r 'c :\Users\admin\Desktop\ test.xlsx ') print(df)Copy the code

result:

Classified Goods Physical store Sales Volume Online Cost of sales Selling Price 0 Fruit Apple 34 234 12 45 1 Home appliance TV 56 784 34 156 2 home appliance refrigerator 78 345 24 785 3 Books Python from getting started to giving up 25 34 13 89 4 Fruit grapes 789 56 7 398Copy the code

1. Arithmetic

Arithmetic operations are basically addition, subtraction, multiplication and division

1.1 Add two columns

Import pandas as pd df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' store sales '] + df[' online sales '])Copy the code

result:

0    268
1    840
2    423
3     59
4    845
dtype: int64
Copy the code

1.2 Subtract two columns

Df = pd read_excel (r 'C: \ Users \ admin \ Desktop \ test. XLSX') print (df [' price '] + df [' costs'])Copy the code

result:

0     57
1    190
2    809
3    102
4    405
dtype: int64
Copy the code

1.3 Multiply two columns

Df = pd read_excel (r 'C: \ Users \ admin \ Desktop \ test. XLSX') print (df [' price '] * (df [' entity shop sales'] + df [' online sales']))Copy the code

result:

0     12060
1    131040
2    332055
3      5251
4    336310
dtype: int64
Copy the code

1.4 Divide two columns

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' store sales '] / df[' online sales '])Copy the code

result:

0     0.145299
1     0.071429
2     0.226087
3     0.735294
4    14.089286
dtype: float64
Copy the code

1.5 Add a constant value to any column

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' price '] + 10)Copy the code

result:

0 55 1 166 2 795 3 99 4 408 Name: dtype: INT64Copy the code

1.6 Subtract a constant value from any column

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' cost '] - 5)Copy the code

result:

0 7 1 29 2 19 3 8 4 2 Name: cost, dtype: INT64Copy the code

1.7 Multiply any column by a constant value

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' online sales '] * 2)Copy the code

result:

0 468 1 1568 2 690 3 68 4 112 Name: dtype: int64Copy the code

1.8 Divide any column by a constant value

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' sales '] / 2)Copy the code

result:

0 17.0 1 28.0 2 39.0 3 12.5 4 394.5 Name: Indicates the number of float64 in a physical store

2. Compare operations

Comparison operations are >, <, =, >=, <=, and so on

Df = pd.read_excel(r'C:\Users\admin\Desktop\ test.xlsx ') print(df[' sales '] > df[' sales '])Copy the code

result:

0    False
1    False
2    False
3    False
4     True
dtype: bool
Copy the code
Df = pd read_excel (r 'C: \ Users \ admin \ Desktop \ test. XLSX') print (df [' price '] < df [' costs'])Copy the code

result:

0    False
1    False
2    False
3    False
4    False
dtype: bool
Copy the code