qq_27866857
2021-01-19 18:33
采纳率: 83.3%
浏览 271
已采纳

python dataframe既有文字又有数字,所有数字列都/100.

 python dataframe既有文字又有数字,列数很多,对数字进行处理,比如最后一列/100,或者所有数字列都/100.

import pandas as pd
d={
    'name':['a','n','c','d','e','f'],
    # 'Gender':['male','female','male','male','female','female'],
    'age':[23,24,24,22,21,20],
    'hight':[173,174,164,172,161,160],
    'weight1':[53,74,44,62,71,60],
    'weight2':[53,64,54,66,81,50]
}
df=pd.DataFrame(d)
df.set_index('name')
print(df)
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • great-sakya 2021-01-19 19:27
    已采纳
    import pandas as pd
    import numpy
    d={
        'name':['a','n','c','d','e','f'],
        # 'Gender':['male','female','male','male','female','female'],
        'age':[23,24,24,22,21,20],
        'hight':[173,174,164,172,161,160],
        'weight1':[53,74,44,62,71,60],
        'weight2':[53,64,54,66,81,50]
    }
    df=pd.DataFrame(d)
    df.set_index('name')
    print(df)
    for i in df:
        if type(df[i][0])==numpy.int64:
            for j in range(len(df[i])):
                df[i][j] = df[i][j]/100
    print(df)
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • coagenth 2021-01-20 02:02

    for i in df.columns:

        if df[i].dtype==np.int64:

            df[i]/=100

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题