here is my code : import pandas as pd import os The path where your CSV files are stored…
- User
here is my code : import pandas as pd import os
The path where your CSV files are stored
csv_folder_path = r"E:\01_calculate_talib\New Folder"
Iterate through each file in the csv_folder_path
for csv_file in os.listdir(csv_folder_path): file_path = os.path.join(csv_folder_path, csv_file)
code# Load the CSV file df = pd.read_csv(file_path) # Get the number of original columns dynamically original_columns_count = len(df.columns) # Generate original column names dynamically (optional, for naming consistency) original_columns = df.columns.tolist() # Creating new column names based on the original question's criteria new_column_names = original_columns.copy() prefixes = ['n7d', 'n6d', 'n5d', 'n4d', 'n3d', 'n2d', 'n1d'] for prefix in prefixes: new_column_names.extend([f"{prefix}_{name}" for name in original_columns]) # Prepare an empty dataframe to hold the transformed data transformed_df = pd.DataFrame(index=df.index, columns=new_column_names) transformed_df = transformed_df.fillna('') # Fill with empty strings (or NaN as you prefer) # Fill in the data for the new structure for i in range(len(df)+6): # Current row data row_data = df.iloc[i].values.tolist() # Add data from preceding 7 rows for j in range(1, 8): if i - j >= 0: row_data = df.iloc[i - j].values.tolist() + row_data else: # Fill with empty strings if there are not enough previous rows row_data = [''] * original_columns_count + row_data transformed_df.iloc[i] = row_data columns_to_remove = transformed_df.filter(like='Unix').columns transformed_df.drop(columns=columns_to_remove, inplace=True) # transformed_df = transformed_df.iloc[7:] # Save transformed dataframe to a new CSV transformed_df.to_csv(file_path, index=False) print("Transformation complete. Check the 'transformed_output_file.csv' file.") - Assistant
- User
this makes my data set reverse
at first its Acsending and after operation became descending
make it ascending before saving
- Assistant
To ensure your dataset remains in ascending order after the transformations and before saving, you simply need to reverse the DataFrame one more time. This can be easily achieved by using the
ilocproperty of the DataFrame. Here's the adjusted part of the script that includes this final step:python# Before saving, reverse the transformed_df to make it ascending again transformed_df = transformed_df.iloc[::-1] # Save transformed dataframe to a new CSV transformed_df.to_csv(file_path.replace(".csv", "_transformed.csv"), index=False) # Change the file name to avoid overwriting the original CSV print(f"Transformation complete for {csv_file}. Check the transformed CSV file.")By adding
transformed_df = transformed_df.iloc[::-1]just before saving, you're essentially reversing the order of the DataFrame rows back to the original ascending order. The slicing operation is a common Python idiom for reversing a list, and it works similarly when applied to pandas DataFrame rows.