我有多个csv文件。其中一些有5列如下:
Date Open High Low Close
2006/01/03 4762.2002 4782.7002 4751.1001 4776.0
2006/01/04 4786.7998 4823.3999 4776.0000 4820.3
2006/01/05 4820.1001 4831.0000 4807.2998 4813.7
2006/01/06 4813.6001 4813.7002 4784.7998 4791.1
2006/01/09 4803.5000 4834.7998 4791.1001 4831.5
其中一些只有两列:
Date Close
01/04/2006 1245.739990
01/05/2006 1246.332593
01/06/2006 1247.555685
01/09/2006 1249.867750
01/10/2006 1253.140732
我希望在一个csv中将这些列合并在一起,并为每个特定文件重命名“Close”列。由于某些文件只有“关闭”,因此在此过程中删除了数据。如何合并,重命名和保留这些文件的“关闭”数据?
这是我之前尝试过的:
import pandas as pd
import numpy as np
for key, value in feature_df_dict.items():
columns_to_delete = ["Open", "High", "Low"]
for column in columns_to_delete:
if column in feature_df_dict[key].columns:
del feature_df_dict[key][column]
col = list(feature_df_dict[key].columns)
print(key)
print(col)
key_name = key.replace("path", "")
key_name = key_name.replace(".csv","")
col = [key_name+"_"+x for x in col if x!= 'Date']
colx = ['Date']
for colv in col:
colx.append(colv)
feature_df_dict[key].columns = colx