...
Python Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ Addition | Adds values on either side of the operator. | 10 + 20 = 30 |
- Subtraction | Subtracts right hand operand from left hand operand. | 10 – 20 = -10 |
* Multiplication | Multiplies values on either side of the operator | 10 * 20 = 200 |
/ Division | Divides left hand operand by right hand operand | 20 / 10 = 2 |
% Modulus | Divides left hand operand by right hand operand and returns remainder | 20 % 10 = 0 |
** Exponent | Performs exponential (power) calculation on operators | 10**20 =10 to the power 20 |
// | Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) − | 9//2 = 4 and 9.0//2.0 = 4.0 -11//3 = -4 -11.0//3 = -4.0 |
Python Comparison Operators
Info |
---|
Below example is based on the condition as a=10, b=20 |
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | (a != b) is true. |
<> | If values of two operands are not equal, then condition becomes true. | (a <> b) is true. This is similar to != operator. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (a > b) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | (a < b) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (a >= b) is not true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | (a <= b) is true. |
Data Structures - List / Set / Tuple / Dictionary
...
Code Block |
---|
# Import pandas as pd import pandas as pd # Import the cars.csv data: cars cars = pd.read_csv('cars.csv') # Print out cars print(cars) |
CSV
Reading a CSV file by Pandas DataFrame with 1st column as index
Code Block |
---|
# Import pandas and cars.csv import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out country column as Pandas Series print(cars['cars_per_cap']) # Print out country column as Pandas DataFrame print(cars[['cars_per_cap']]) # Print out DataFrame with country and drives_right columns print(cars[['cars_per_cap', 'country']]) |
Save a Pandas DaraFrame by CSV format
Code Block |
---|
dict = {"country": ["Brazil", "Russia", "India", "China", "South Africa"], "capital": ["Brasilia", "Moscow", "New Dehli", "Beijing", "Pretoria"], "area": [8.516, 17.10, 3.286, 9.597, 1.221], "population": [200.4, 143.5, 1252, 1357, 52.98] } import pandas as pd brics = pd.DataFrame(dict) brics.to_csv('example.csv') |
Save a Pandas DaraFrame by CSV format with header and no index
Code Block |
---|
from pandas import DataFrame Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'], 'Price': [22000,25000,27000,35000] } df = DataFrame(Cars, columns= ['Brand', 'Price']) export_csv = df.to_csv (r'C:\Users\Ron\Desktop\export_dataframe.csv', index = None, header=True) #Don't forget to add '.csv' at the end of the path print (df) |
Print partial rows (observations) from a Pandas DataFrame
Code Block |
---|
# Import cars data import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out first 4 observations print(cars[0:4]) # Print out fifth, sixth, and seventh observation print(cars[4:6]) |
...
Code Block |
---|
# Import cars data import pandas as pd cars = pd.read_csv('cars.csv', index_col = 0) # Print out observation for Japan print(cars.iloc[2]) # Print out observations for Australia and Egypt print(cars.loc[['AUS', 'EG']]) |
Sort
Sort a Pandas DataFrame in an ascending order
Info |
---|
df.sort_values(by=['Brand'], inplace=True) |
...
Code Block |
---|
# sort - ascending order from pandas import DataFrame Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'], 'Price': [22000,25000,27000,35000], 'Year': [2015,2013,2018,2018] } df = DataFrame(Cars, columns= ['Brand', 'Price','Year']) # sort Brand - ascending order df.sort_values(by=['Brand'], inplace=True) print (df) |
Sort a Pandas DataFrame in a descending order
Info |
---|
df.sort_values(by=['Brand'], inplace=True, ascending=False) |
...
Code Block |
---|
# sort - descending order from pandas import DataFrame Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'], 'Price': [22000,25000,27000,35000], 'Year': [2015,2013,2018,2018] } df = DataFrame(Cars, columns= ['Brand', 'Price','Year']) # sort Brand - descending order df.sort_values(by=['Brand'], inplace=True, ascending=False) print (df) |
Sort a Pandas DataFrame by multiple columns
Info |
---|
df.sort_values(by=['First Column','Second Column',...], inplace=True) |
...