나는 목록으로 딕셔너리과 회사로 제공하는 데이터에 대한 몇 가지 가정하에 코드를 고정 파이썬 결과 회사 First_Column_Value Second_Column_Value ABC 새로운 오전. 년 및 회사 이름을 자유롭게 바꾸십시오. 그렇게하면 DataFrame의 조 변경을 사용할 필요가 없습니다.
하는 것은 더 설명은 코드의 주석을 참조하십시오 :
import pandas as pd
# sample data
company_names = ['Company A','Company B','Company C']
data = {"2010" : [0,100,230], "2011" : [120,0,300], "2012" : [130,240,0]}
# create DataFrame
df = pd.DataFrame(data, index=col_names)
# since the data is not provided in the correct way (rows and columns are swapped)
# we need to get the transpose of the DataFrame before further processing
df = df.T
# sort index in order to make sure that years are sorted chronologically
df.sort_index(inplace=True)
print(df)
# iterate through all columns and get the first index element where condition applies
# and store in dict
out = {}
for col in df:
out[col] = df[df[col] > 0].index.tolist()[0]
print(out)
는 출력으로주기 : 회사 B는 계산하기 위해서는 2010 년
년에 처음으로 예약을했다
Company A Company B Company C
2010 0 100 230
2011 120 0 300
2012 130 240 0
{'Company B': '2010', 'Company A': '2011', 'Company C': '2010'}
그래서 예를 들어
동적으로
CAGR 종류의 시간 간격을 알아야합니다.이 시간 간격을 알고 있어야하며 매년 데이터를 보유해야합니다. 다른 방법은 timestamp를 사용하여 timestamp 인덱스를 사용하고 간격을 계산하는 것입니다.
# assume to have a time interval of one year
delta_t = 1
# in order to divide to rows we apply `df.div()` which basically divides two DataFrames.
# To divide each row with the following row we apply `df.shift(1)` to the same DataFrame
# in order to shift the DataFrame by one row (see docs on used commands for futher details).
cagr = ((df.div(df.shift(1)))**(1/delta_t) -1)*100
print(cagr)
이주기 : 간단하게하기 위해서
난 당신이 매년 하드 코드 일년의 시간 간격에 대한 전체 데이터를 가지고 있는지 확인 할 수 있습니다 가정
Company A Company B Company C
2010 NaN NaN NaN
2011 inf -100.000000 30.434783
2012 8.333333 inf -100.000000
필터링 이 데이터는 NaN
(또는 심지어 inf
)의 CAGR을 갖는 것이 경제적 관점에서별로 의미가 없기 때문에이 시점부터 적용 가능한 결과를 얻으려면이 시점까지해야합니다.
데이터 프레임 생성자에 대한 코드를 수정할 수 있습니까? 그건 유효한 사전이 아니야. – ayhan
안녕하세요, 귀하의 질문을받지 못해 죄송합니다. 내가 Python을 처음 사용하는 것에 대해 미안하다거나 프로그래밍을 말할 수 있다니 유감스럽게 생각합니다. –
@PrankitRa 코드에서 구문을 수정해야합니다. 지금 당장은 실행되지 않을 것입니다 – sirfz