Вот мой код ниже.
Я хочу открыть каждый отчет в своей папке и скопировать все, что мне нужно, в новый отчет.
Но ошибка говорит:
PORT_result("Institution_MTD_9_28", "G:\Risk\Attribution\Fixed_Income\PORT attribution", "Daily_Performance_9_30.xlsx")
Traceback (most recent call last):
File "<ipython-input-2-c886cd1d12e8>", line 1, in <module>
PORT_result("Institution_MTD_9_28", "G:\Risk\Attribution\Fixed_Income\PORT attribution", "Daily_Performance_9_30.xlsx")
File "G:/Risk/Attribution/Fixed_Income/PORT attribution/PORT comparison.py", line 49, in PORT_result
dt.columns = range(5)
File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\generic.py", line 4385, in __setattr__
return object.__setattr__(self, name, value)
File "pandas\_libs\properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\generic.py", line 645, in _set_axis
self._data.set_axis(axis, labels)
File "C:\Users\pjia\AppData\Local\Continuum\anaconda2\lib\site-packages\pandas\core\internals.py", line 3323, in set_axis
'values have {new} elements'.format(old=old_len, new=new_len))
ValueError: Length mismatch: Expected axis has 4 elements, new values have 5 elements
Может кто-нибудь помочь, пожалуйста?
def PORT_result(folder_name, location, daily_name):
root = location + "\\" + folder_name # Set directory
os.chdir(root) # Change current working directory to our folder directory
report_name = fnmatch.filter(os.listdir('.'), '*.xls') # List all the reports' name under this folder
file_path = []
for i in range(len(report_name)):
path = [root + "\\" + report_name[i]]
file_path = file_path + path # Get the full file path of all reports
result = []
for m in range(len(report_name)):
d = file_path[m]
df = pd.read_excel(d,'Global Portfolio Summary')
df = list(df.index.values)
df = df[2] + df[7] + df[8] + df[9] + df[10] + df[11] + df[12] + df[13] + df[14] + df[15]# Slice the list and keep return info
df = list(df)
df = [x for x in df if pd.isnull(x) == False] # Remove NaN
df = [x for x in df if x != "Global Outperformance Details"]
idx = [j for i, j in enumerate(df) if i % 2 == 0 and i != 0]
value = [j for i, j in enumerate(df) if i % 2 != 0 and i != 1]
fund_name = df[0][11:]
period = df[1][7:]
value = [fund_name] + [period] + value
idx = [unicode('Fund')] + [unicode('Period')] + idx
df2 = pd.DataFrame(data = value)
df2 = df2.T
df2.columns = idx
result = result + [df2] # Concat the result together
dt = pd.read_excel(daily_name)
dt.iloc[0,0] = 'name'
dt.index = dt.iloc[:,0]
dt.columns = range(5)
dt = dt.drop(0,axis = 1)
dt.columns = ['FMTD','OMTD','FYTD','OYTD']
dt = dt.drop('name', axis = 0)
dt = dt.dropna(how = 'all')
dt2 = pd.read_excel('Namelist.xlsx')
writer = pd.ExcelWriter(folder_name + '.xlsx') # Write the dataframe into excel
output = pd.concat(result, sort=False)
output.index = range(1,len(output)+1)
PORT_result("Institution_MTD_9_28", "G:\Risk\Attribution\Fixed_Income\PORT attribution", "Daily_Performance_9_30.xlsx")