I have two .csv files that I am operating by loading into two different data frames (df_A and df_B). Operations are for example deducting one column from one another column in the second data frame. All data is keyed by a category and thus I am assuming that the following can work if the underlying index remains the same.
df_result["R"] = pd.to_numeric(df_B["B"].str.replace('%', '')) - pd.to_numeric(df_A["A"].str.replace('%', ''))
Each file has the same number of rows, but how can I ensure that the ordering remains the same when accessing df_B["B"]? I have tried to set the index with set_index("Category", inplace = True), but later on operations that access df_A["Category"] fail with the following error:
KeyError: 'Line'
This occurs when I am trying to set a brand new data frame to use the same values, presumably because this is an index.
df_result["Category"] = df_A["Category"]
Is there a smarter way of creating a new data frame to store the results generated from dataframes A and B?
What is the best way of ensuring that rows in frame A relate to the very same row in frame B? Without set_index, am I just taking this for granted?