script for merging csv

This commit is contained in:
prisar
2023-07-14 16:56:28 +05:30
parent 521eb235d5
commit 1f65c08ed9
3 changed files with 135 additions and 7 deletions

37
scripts/merge_csv.py Normal file
View File

@@ -0,0 +1,37 @@
import glob
import pandas as pd
import openpyxl
# list all csv files only
csv_files = glob.glob('*.{}'.format('csv'))
# print(csv_files)
df_csv_append = pd.DataFrame()
first = True
# merge the CSV files
for file in csv_files:
if first:
df_csv_append = pd.read_csv(file)
colname = file.split('.')[0]
df_csv_append.rename(columns={'ca': colname}, inplace = True)
df_csv_append = df_csv_append.drop(['1'], axis=1)
first = False
else:
df = pd.read_csv(file)
colname = file.split('.')[0]
df.rename(columns={'ca': colname}, inplace = True)
df = df.drop(['1'], axis=1)
df_csv_append = df_csv_append.merge(df, on='tv')
print(df_csv_append[df_csv_append['tv'].between(300, 700)])
df_csv_append = df_csv_append[df_csv_append['tv'].between(300, 700)]
df_csv_append.rename(columns={'tv': "123_tv"}, inplace = True)
df_csv_append = df_csv_append.reindex(sorted(df_csv_append.columns), axis=1)
df_csv_append.to_excel("D27 Merged.xlsx", index=False)
df_csv_append.to_csv("D27 Merged.csv", index=False)