Pandas

Pandas (53)

Jun's N 2022. 9. 20. 21:37

How to get the last n rows of a dataframe with row sum > 100?

# Input
df = pd.DataFrame(np.random.randint(10, 40, 60).reshape(-1, 4))

# Solution
# print row sums
rowsums = df.apply(np.sum, axis=1)

# last two rows with row sum greater than 100
last_two_rows = df.iloc[np.where(rowsums > 100)[0][-2:], :]
728x90