Created
June 18, 2021 08:00
-
-
Save sulaya86/40fb48de6c1bd7e30684c07728ff0758 to your computer and use it in GitHub Desktop.
For a given dataset from a Excel File, Create files per a desired column, each file is named as column and today's date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ~ | |
""" | |
For a given dataset from a Excel File, | |
Create files per a desired column, | |
each file is named as column and today's date | |
""" | |
# Author: Soraya Ruiz | |
# Creation Date: 2021-06-18 | |
import pandas as pd | |
import os | |
import time | |
# Current location of main.py | |
__location__ = os.path.realpath( | |
os.path.join(os.getcwd(), os.path.dirname(__file__))) | |
# File name of the customer data to be split | |
file_path = os.path.join(__location__, "CustomersDetails.xlsx") | |
def create_file_per_dataset(data): | |
for customer_id, frame in data: | |
print(f"First 2 entries for {customer_id!r}") | |
print("------------------------") | |
print(frame.head(), end="\n\n") | |
today_date = time.strftime("%d-%m-%Y") | |
filename = str(customer_id) + "_" + today_date + ".xlsx" | |
frame.to_excel(filename, index=False) | |
def extract_by_column(dataset, column_name): | |
by_customer = dataset.groupby(column_name) | |
return by_customer | |
def create_dataset_from_file(): | |
data = pd.read_excel(file_path) | |
return data | |
def main(): | |
""" | |
Entry point of the solution | |
""" | |
dataset = create_dataset_from_file() | |
print(create_file_per_dataset(extract_by_column(dataset, "Customer ID"))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gracias por el apoyo :)