0

Hi I am adding specific cells in a row. In some cases they are a continuous range but never the entire row! This is why I'm wondering if there is a better way of doing this than:

import pandas as pd, numpy as np

mininmal_array = np.random.rand(3,3)

min_df = pd.DataFrame(mininmal_array, columns=['H', 'E', 'C'], index=['H', 'E', 'C'])
display(min_df)

# Summing only field E and C from row H:
sum_EC = min_df['E']['H']+min_df['C']['H']
print(sum_EC)

Is there a more elegant way of doing this? I mean whenever the fields are adjacent. The important point is that I do NOT want to sum the entire row.

What would be the way of doing this with a colum? E.g In column 'E' Summing exclusively filed EE and EC:

sum_col = min_df['E']['E']+min_df['E']['C']
print(sum_col)
# 0.937706262999336

I am also wondering if there is a way to sum all cells that do not have matching index and column. E.g. sum all cells were column!=['E'] AND index!=['E'].

Thanks for your constructive answers in advance.

ilam engl
  • 1,310
  • 1
  • 9
  • 22
  • The linked answer does NOT answer (if you've even read it). The point is to add parts of a row but NOT the entire row. Parts of a column but NOT the entire column. – ilam engl Nov 18 '20 at 13:15
  • might be easier with a longer dataframe, or even a multi index try `pd.melt(min_df.reset_index(),id_vars='index')` – Umar.H Nov 18 '20 at 13:28

0 Answers0