加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python学习:pandas行最大值问题

(2021-04-07 08:36:23)
标签:

python

编程

关于pandas 按行求最大值问题----得不到想要的结果,不知何故

import pandas as pd
df1=pd.DataFrame([
                [1,2,3,4,5,6,7,8,9,10,11,12,13,'a'],
                [2,3,4,5,6,7,8,9,10,11,12,13,'b',15],
                [3,4,5,6,7,8,9,10,11,12,13,14,'c','d']
                ])
df1['max']=df1.max(axis=1)
print(df1)

运行结果如下:
          10  11  12  13  max
    10  11  12  13     12
  10  11  12  13    15   13
10  11  12  13  14       14
--------
df1=pd.DataFrame([
                [1,2,3,4,5,6,7,8,9,10,11,12,13,'a'],
                [2,3,4,5,6,7,8,9,10,11,12,13,'b',15],
                [3,4,5,6,7,8,9,10,11,12,13,14,15,'d']
                ])
df1['max']=df1.max(axis=1)
print(df1)

运行结果如下:
          10  11  12  13  max
    10  11  12  13     12
  10  11  12  13    15   13
10  11  12  13  14  15     14
-------------------
df1=pd.DataFrame([
                [1,2,3,4,5,6,7,8,9,10,11,12,'a',14],
                [2,3,4,5,6,7,8,9,10,11,12,13,'b',15],
                [3,4,5,6,7,8,9,10,11,12,13,14,'c',16]
                ])
df1['max']=df1.max(axis=1)
print(df1)
运行结果如下:
          10  11 12  13  max
    10  11  12  14   14
  10  11  12  13  15   15
10  11  12  13  14  16   16
------
df1=pd.DataFrame([
                [1,2,3,4,5,6,7,8,9,10,11,'a',13,14],
                [2,3,4,5,6,7,8,9,10,11,12,13,'b',15],
                [3,4,5,6,7,8,9,10,11,12,13,14,15,'c']
                ])

df1['max']=df1.max(axis=1)
print(df1)
运行结果如下:
          10  11  12  13  max
    10  11    13  14   11
  10  11  12  13    15   12
10  11  12  13  14  15     13
------------------------------------------------------
可能原因分析:
末列若含有字符且其前面紧邻的列中某行也含有字符的列,不参与统计。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有