import pandas as pd
import numpy as np
def DataframeMinTest():
df = pd.DataFrame(np.random.randn(1000, 3), columns=list('ABC'))
print(df.shape)
# 方式一 取AB两列小值合成D列
df['D'] = df.loc[:, ['A', 'B']].min(axis=1)
# 方式二 取AC两列大值合成E列
df['E'] = np.max(df.loc[:, ['A', 'C']].values, axis=1)
print(df.head(10))
if __name__ == '__main__':
DataframeMinTest()
输出
(1000, 3) ? ? ? ? ? A? ? ? ? ? ? ? ?B? ? ? ? ? ? ? ?C? ? ? ? ? ? ? ?D? ? ? ? ? ? ?E 0 -0.879277 -1.027903 -0.602419 -1.027903 -0.602419 1 ?1.010298 ?1.604261 -0.097757 ?1.010298 ?1.010298 2 -0.228731 ?0.646538 ?0.020998 -0.228731 ?0.020998 3 ?1.404384 -1.681164 ?0.134697 -1.681164 ?1.404384 4 -0.368503 ?0.270716 ?1.419268 -0.368503 ?1.419268 5 ?2.238605 ?1.628516 -1.984287 ?1.628516 ?2.238605 6 ?0.652273 -0.166380 ?1.855101 -0.166380 ?1.855101 7 ?0.602067 ?0.604722 -0.640275 ?0.602067 ?0.602067 8 -2.775687 -2.649085 ?0.502292 -2.775687 ?0.502292 9 ?0.031345 ?0.419377 -0.445941 ?0.031345 ?0.031345
|