I have the following data
ticker company sector industryGroup industry subindustry currency 1999-07-31 1999-10-31 2000-01-31 ...
CompA Health Health Health Health Health USD 12.3 23.33 33.1 ...
CompB Machine Machine Machine Machine Machine USD 32.1 34.44 23.1 ...
CompC Machine Machine Machine Machine Machine USD 32.1 34.44 23.1 ...
CompD Machine Machine Machine Machine Machine USD 32.1 34.44 23.1 ...
The above is just a sample of the data that is in excel file. The prices go till 02-01-2024 and there are more company row 541 companies to be exact. I wrote a code that takes the columns starting from the first date and puts them in a dataframe with the date column title. Next I took the second column prices and put them in a column with ticker symbol name. The output should be the same as below.
Date CompA CompB CompC CompD
1999-07-31 12.3 32.1 32.1 32.1
1999-10-31 23.33 34.44 34.44 34.44
2000-01-31 33.1 34.44 34.44 34.44
The is my code:
import pandas as pd
bvmf = pd.read_excel('Market.xlsx')
df = pd.DataFrame()
df['Date'] = bvmf.iloc[0:1, 10:].columns
for i in range(len(bvmf)):
ticker = bvmf['ticker'][i]
df[ticker] = bvmf.iloc[i:i+1, 10:].values[0]
There are alot of values. My question, is there a better way to implement this code?
mahmoud988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.