0
votes

I want to merge two dataframes togehter. The first dataframe is created by adding three dataframes together. These dataframes contain the same data for different companies.

df_ipos_401 = pd.read_csv("C:/Users/niklas/Documents/BA/Daten/Daten-Regression/401_IPO_data_maximal_csv.csv", header = 0, usecols=['CUSIP9From6_norm','Issuer', 'IssueDate', 'MainSICCode'])
df_ipos_401.columns= (['Issuer','FilingDate', 'SIC-Code', 'Cusip'])
df_ipos_402 = pd.read_csv("C:/Users/niklas/Documents/BA/Daten/Daten-Regression/402_IPO_data_maximal_EDGAR_match.csv", header = 0, usecols=['CUSIP9From6_norm','Issuer', 'IssueDate', 'MainSICCode'])
df_ipos_402.columns= (['Issuer','FilingDate', 'SIC-Code', 'Cusip'])
df_ipos_100 = pd.read_csv("C:/Users/niklas/Documents/BA/Daten/Daten-Regression/100_IPO_data_merged_by_DealNumber_without_any_exclusion.csv", header = 0, usecols=['CUSIP9From6_norm','Issuer', 'IssueDate', 'MainSICCode'])
df_ipos_100.columns= (['Issuer','FilingDate', 'SIC-Code', 'Cusip'])

result_ipos = pd.concat([df_ipos_401,df_ipos_402,df_ipos_100])

df_prep_ipo = result_ipos.drop_duplicates()

This is the output:

    Issuer                          FilingDate      SIC-Code    Cusip9
0   Sea Pines Co                    1973-01-09      7011      811414101
1   Teltronics Services             1973-01-09      1711      879699106
2   Syracuse China Corp             1973-01-11      3262      871668109
3   Universal Security Instruments  1973-01-16      3669      913821104
4   WD-40 Co                        1973-01-16      2899      929236107

To this new dataframe now I want to fill the balance sheet figures for the year of the filing data. The other dataframe (df_fund) with the financial data in looks like this:

    Year    Cusip9      Name       Total Assets Debt       Sales        Income      LogSales    Leverage    Asset Turnover Margin   Revenue Growth
0   2010.0  000361105   AAR CORP    17037270.0  3298020.0   731390.0    17757820.0  13.502702   0.193577    0.042929    24.279550   NaN
1   2011.0  000361105   AAR CORP    21956530.0  6694890.0   677230.0    20744980.0  13.425766   0.304916    0.030844    30.632104   -0.074051
2   2012.0  000361105   AAR CORP    21369000.0  6222000.0   550000.0    21671000.0  13.217674   0.291169    0.025738    39.401818   -0.187868
3   2013.0  000361105   AAR CORP    21995000.0  5643000.0   729000.0    20350000.0  13.499429   0.256558    0.033144    27.914952   0.325455
4   2014.0  000361105   AAR CORP    15150000.0  850000.0    -545000.0   15943000.0  NaN 0.056106    -0.035974   -29.253211  -1.747599

The code I tried is this:

df_prep_ipo['IPO Year'] = pd.DatetimeIndex(df_prep_ipo['Year']).year

df_ipos = df_prep_ipo.merge(df_fund, left_on=['Cusip9', 'IPO Year'], right_on=['Cusip9', 'Year'])

del df_ipos['Name']
del df_ipos['Year']

The code works for the single dataframes, but after I added the 3 dataframes to one it does not work any more. I receive is this error message: KeyError: 'Year' I assume that for some companies I don't have the financial data for their respective IPO Year. So I guess it would make the most sense to drop these companies. Does anyone know how I can fix it?

1

1 Answers

0
votes

I believe the error is occurring because df_prep_ipo and df_fund do not have the column Year. So, we need to first extract year from the FilingDate of df_prep_ipo and change the column name of df_fund from Year to IPO Year. Also, it would be better to change df_fund year to int.

Try the code below before merging.

df_prep_ipo['IPO Year'] = pd.DatetimeIndex(df_prep_ipo['FilingDate']).year
df_fund = df_fund.rename(columns={'Year': 'IPO Year'})
df_fund['IPO Year'] = df_fund['IPO Year'].astype(int)

If you want to remove rows missing Year data, preprocess data using the following code before running the part above.

df_prep_ipo = df_prep_ipo.dropna(subset=['FilingDate'])
df_fund = df_fund.dronpa(subset=['Year'])