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)): # 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
AttributeError: 'DataFrame' object has no attribute 'append'. Did you mean: '_append'?
- Assistant