假设您有一个数据框,
one two three 0 12 13 5 1 10 6 4 2 16 18 20 3 11 15 58
将最小值存储在新行和新列中的结果是-
Add new column to store min value one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 Add new row to store min value one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 4 10 6 4 4
为了解决这个问题,我们将遵循以下步骤-
定义一个数据框
计算每一列中的最小值,并使用以下步骤将其存储为新列:
df['min_value'] = df.min(axis=1)
在每一行中找到最小值,然后使用以下步骤将其存储为新行,
df.loc[len(df)] = df.min(axis=0)
让我们看一下下面的实现以更好地理解,
import pandas as pd import numpy as np data = [[12,13,5],[10,6,4],[16,18,20],[11,15,58]] df = pd.DataFrame(data,columns=('one','two','three')) print("Add new column to store min value") df['min_value'] = df.min(axis=1) print(df) print("Add new row to store min value") df.loc[len(df)] = df.min(axis=0) print(df)
Add new column to store min value one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 Add new row to store min value one two three min_value 0 12 13 5 5 1 10 6 4 4 2 16 18 20 16 3 11 15 58 11 4 10 6 4 4