Add source example files
This commit is contained in:
36
source/pandas/01-pandas-files/01-json-demo-communes.py
Normal file
36
source/pandas/01-pandas-files/01-json-demo-communes.py
Normal file
@ -0,0 +1,36 @@
|
||||
import pandas as pd
|
||||
from matplotlib import pyplot as plt
|
||||
import matplotlib
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Configure Pandas to only display floats not in scientific notation
|
||||
pd.options.display.float_format = "{:.2f}".format
|
||||
# Read a list of FR lowest administrative divisions
|
||||
# See france-communes-readme.md for field informations
|
||||
df = pd.read_json("../data/json/france-communes.json")
|
||||
# Get the mean of columns as a new row, and only keep the population column
|
||||
df1 = df.copy()
|
||||
df1.loc["mean"] = df1.mean(skipna=True, numeric_only=True)[["population"]]
|
||||
# Show dataframe
|
||||
print(df1.to_string(max_cols=9))
|
||||
|
||||
# Get only cities in the dept 94
|
||||
df2: pd.DataFrame = df.copy()
|
||||
# Filter rows by using criteria
|
||||
df2 = df2.loc[(df2["departement"] == "94") & (df2["population"] > 0) & (df2["nom"].str.contains(""))]
|
||||
# Sort data by columns
|
||||
df2.sort_values("nom", inplace=True)
|
||||
# Remove columns from the dataframe
|
||||
df2.drop(columns=["chefLieu", "commune"], inplace=True)
|
||||
print(df2.to_string(max_cols=10))
|
||||
|
||||
# Show lines
|
||||
matplotlib.use("QtCairo")
|
||||
plt.rcParams["figure.dpi"] = 96
|
||||
gp = df2.plot.line(x="nom", y="population")
|
||||
plt.show()
|
||||
|
||||
# Get postal codes from line 37237
|
||||
row = df2.loc[37237]
|
||||
print(row["codesPostaux"], type(row["codesPostaux"]))
|
||||
|
Reference in New Issue
Block a user