import pandas as pd
def synchronize_csv_files(old_csv_path, new_csv_path, output_csv_path):
# Read the old and new CSV files into DataFrames
old_df = pd.read_csv(old_csv_path)
new_df = pd.read_csv(new_csv_path)
# Find the matching value in the first column of the second row in the new CSV
matching_value = old_df.iloc[1, 0]
# Delete rows from the new CSV until the matching value is found in the first column
while new_df.iloc[1, 0] != matching_value:
new_df = new_df.iloc[1:]
# Reset the index of the new DataFrame
new_df.reset_index(drop=True, inplace=True)
# Combine the rows from both CSV files into a single DataFrame
combined_df = pd.concat([old_df, new_df], axis=1)
# Save the synchronized CSV to the output file
combined_df.to_csv(output_csv_path, index=False)
def calculate_numbers(input_csv_path, output_csv_path):
# Read the synchronized CSV file into a DataFrame
df = pd.read_csv(input_csv_path)
# Calculate the result in the 6th column based on the values in the 2nd and 5th columns
df['GAP'] = df['Views.1'] - df['Views']
# Save the modified CSV to the output file with a new name
df.to_csv(output_csv_path, index=False)
# Example usage:
old_csv_path = "/Users/jitendersingh/Downloads/Smart Enough 2 nov.csv"
new_csv_path = "/Users/jitendersingh/Downloads/Smart Enough 3 nov.csv"
output_csv_path = "/Users/jitendersingh/Downloads/temp_csv.csv"
synchronize_csv_files(old_csv_path, new_csv_path, output_csv_path)
# Example usage:
input_csv_path = "/Users/jitendersingh/Downloads/temp_csv.csv"
output_csv_path = "/Users/jitendersingh/Downloads/synchronized_csv_with_numbers.csv"
calculate_numbers(input_csv_path, output_csv_path)
0 comments:
Post a Comment