I have four matrices. I structured it with Panda with column and row names. I want to sum four matrices with the same row and column name to get 8x8 matrix. To make it clear;
Matrix 1: row 1 column 1 -> 1
Matrix 3: row 1 column 1 -> 0.64
I want to sum only 1 and 0.64. So in my new 8x8 matris row 1 column 1 -> 1.64
Sorry for my weak English. I hope you understand.
I tried if condition but I couldn't make up a good if condition.
matris = pd.DataFrame(matris, columns=column_names, index=row_names)
You can use the add
method (see here ) because it allows "addition of dataframe and other, element-wise".
Thus, if your four DataFrameare named df1, df2, df3 and df4, you can add them up in this way:
df1.add(df2.add(df3.add(df4)))
if all the dataframes have the same indexes and columns, that will work perfectly for you. Otherwise, you could just make a sum of the values of each DataFrame:
df1.values + df2.values + df3.values + df4.values
Such solution will work nicely if all the DataFrames have the same shape, regardless if they different column names and/or index labels.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.