8000 2024/03/30 - Updating Demo python files · rphadtare/python_by_examples@ee3c76e · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit ee3c76e

Browse files
committed
2024/03/30 - Updating Demo python files
1 parent 978757c commit ee3c76e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

advanced/PythonDemo_05_Pandas_module.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import random
33

44
import pandas as pd
5-
5+
import os
66

77
def seriesDemo():
88
"""This function gives demo for Pandas Series"""
@@ -243,4 +243,50 @@ def dataframe_merge_demo():
243243
print(emp_df.merge(dept_df.rename(columns={'id': 'dept_id'}), how='left'))
244244

245245

246-
dataframe_remove_rows()
246+
def datframe_export_file():
247+
emp_data = {
248+
'emp_id': [10, 20, 30, 40, 50, 60],
249+
'emp_name': ["Rohit", "Pooja", "Rajani", "Rushi", "Rutu", "Prithvi"],
250+
'emp_sal': [5600, 6200, 7900, 7623.45, 5823.41, 5399.14],
251+
'dept_id': [1, 2, 3, 1, 3, 3]
252+
}
253+
254+
dept_data = {
255+
'dept_id': [1, 2, 3],
256+
'dept_name': ["IT", "Civil", "Computer Science"]
257+
}
258+
259+
emp_df = pd.DataFrame(emp_data)
260+
dept_df = pd.DataFrame(dept_data)
261+
262+
merged_df = emp_df.merge(dept_df, how='inner')
263+
path_to_save = os.getcwd() + "/data/employee_details/"
264+
265+
is_dir_exist = os.access(path_to_save, os.F_OK)
266+
if not is_dir_exist:
267+
print("creating directory to store file")
268+
os.makedirs(path_to_save)
269+
270+
merged_df.to_csv(path_to_save+"employee.csv", header=True)
271+
merged_df.to_json(path_to_save+"employee.json")
272+
print("Dataframe saved into csv and json format")
273+
274+
275+
def dataframe_read_json_export_to_parquet():
276+
json_file_path = os.getcwd() + "/data/employee_details/employee.json"
277+
parquet_file_path = os.getcwd() + "/data/employee_details/parquet/"
278+
df = pd.read_json(json_file_path)
279+
280+
print("Employee dataframe ...")
281+
print(df)
282+
print("Saving dataframe to json format")
283+
is_dir_exist = os.access(parquet_file_path, os.F_OK)
284+
if not is_dir_exist:
285+
print(f"creating {parquet_file_path} to store parquet file")
286+
os.makedirs(parquet_file_path)
287+
288+
df.to_parquet(parquet_file_path+"employee.parquet", compression="snappy", engine='fastparquet')
289+
print("File stored as parquet format")
290+
291+
292+
dataframe_read_json_export_to_parquet()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
,emp_id,emp_name,emp_sal,dept_id,dept_name
2+
0,10,Rohit,5600.0,1,IT
3+
1,20,Pooja,6200.0,2,Civil
4+
2,30,Rajani,7900.0,3,Computer Science
5+
3,40,Rushi,7623.45,1,IT
6+
4,50,Rutu,5823.41,3,Computer Science
7+
5,60,Prithvi,5399.14,3,Computer Science
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"emp_id":{"0":10,"1":20,"2":30,"3":40,"4":50,"5":60},"emp_name":{"0":"Rohit","1":"Pooja","2":"Rajani","3":"Rushi","4":"Rutu","5":"Prithvi"},"emp_sal":{"0":5600.0,"1":6200.0,"2":7900.0,"3":7623.45,"4":5823.41,"5":5399.14},"dept_id":{"0":1,"1":2,"2":3,"3":1,"4":3,"5":3},"dept_name":{"0":"IT","1":"Civil","2":"Computer Science","3":"IT","4":"Computer Science","5":"Computer Science"}}
Binary file not shown.

0 commit comments

Comments
 (0)
0