'Reindexing only valid with uniquely valued Index objects doesn't make sense
Nothing I've read about this error has made it any clearer. I have two dataframes with different header names even though they are the same. I want to append one into the other but I keep getting:
pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects
From what I've been able to gather this means that there are multiple indicies values that are the same within each individual dataframe. One answer recommended to do this:
df3 = df3.loc[~df3.index.duplicated(keep='first')]
for each dataframe but it doesn't make any difference.
Here is the code and heads of each df. I would really appreciate anyone being able to help me understand this:
df2company_col = get_company_name(df2)
df2website_col = get_website(df2)
df2postal_col = get_five_postal(df2)
df2phone_col = get_phone_column(df2)
df2state_col = get_state_column(df2)
df2 = df2[[df2company_col, df2website_col, df2postal_col, df2phone_col, df2state_col]]
df2 = df2.loc[~df2.index.duplicated(keep='first')].reset_index(drop=True)
print(df2.index)
print(df2.head())
df3company_col = get_company_name(df3)
df3website_col = get_website(df3)
df3postal_col = get_five_postal(df3)
df3phone_col = get_phone_column(df3)
df3state_col = get_state_column(df3)
dct = {df3company_col: df2company_col,df3website_col:df2website_col ,df3postal_col: df2postal_col, df3phone_col: df2phone_col, df3state_col: df2state_col}
df3 = df3[[df3company_col, df3website_col, df3postal_col, df3phone_col, df3state_col]]
df3.rename(columns=dct,inplace=True)
df3 = df3.loc[~df3.index.duplicated(keep='first')].reset_index(drop=True)
print(df3.index)
print(df3.head())
# When I uncomment the two lines below is when the error throws
# df_leads_accounts = pd.concat([df2, df3], axis=0)
# print(df_leads_accounts)
Here are the outputs of the print statements:
RangeIndex(start=0, stop=1128291, step=1)
Company / Account Website Zip/Postal Code Phone State/Province
0 KAREN GREEN CONSULTING LLC NaN NaN 9046140587 Florida
1 MEMBERS CREDIT UNION NaN 27103 3367749997 North Carolina
2 FYZICAL THERAPY BALANCE CENTER TYLER http://www.fyzical.com/tyler 75703 9033639252 Texas
3 GLASFLOSS INDUSTRIES glasfloss.com 75226 NaN Texas
4 PAWN 2 CASH https://pawn2cash.org/ 47714 8124773781 Indiana
RangeIndex(start=0, stop=622914, step=1)
Company / Account Website State/Province Phone State/Province
0 GREENIX PEST CONTROL SOUTH 800 EAST OREM UT USA NaN NaN 1385219070 NaN
1 SMASH MY TRASH www.smashmytrash.com 16464 1636368749 16464
2 WALLBEDS N MORE INC www.wallbedsnmore.com NaN 6198861828 NaN
3 R S MECHANICAL www.comfortguy.com NaN 9193028956 NaN
4 FINAL FRONTIER RESCUE PROJECT www.finalfrontierrescueproject.org NaN 1512817944 NaN
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|