FOR循环 – 仅在函数的第一次迭代中提供结果。

huangapple go评论71阅读模式
英文:

FOR Loop -only giving result for 1st iteration in function

问题

FOR循环仅在第一次迭代时给出结果,然后停止,不显示任何错误,
在查找pandas.dataframe的中位数时

def construct_frame(x):
    
    '''constructor function to get median of any data'''

    if x.shape[1] >= 0:
        for i in range(1, (x.shape[1])):
            print(i)
            x_ser = x.iloc[:, i].sort_values(ignore_index=True)  # 按降序排序值

            x_ser_new = pd.Series(x_ser)

    #             print(x_ser)
    #             print(len(x_ser))
    #             print(type(x_ser))
    #             print(x_ser.dtypes)
    #             print(x_ser.shape[0])

            if len(x_ser_new) % 2 == 0:  # 检查数据的长度是否为'偶数'

                res = (x_ser_new[len(x_ser_new) // 2] + x_ser_new[(len(x_ser_new) // 2) - 1]) / 2  # 第n列和(n-1)列的平均值
                return res

            else:  # 如果数据的长度为'奇数'

                res = x_ser_new[len(x_ser_new) // 2]  # 第n列
                return res


construct_frame(df)

结果应该是10列,但仅显示第1列的中位数结果:

1
34.26

英文:

FOR loop is only giving result for first iteration then it stops and doesn't show any error also,
while finding median of pandas.dataframe

def construct_frame(x):
    
    '''constructor function to get median of any data'''

    if x.shape[1]>=0:
        for i in range(1,(x.shape[1])):
            print(i)
            x_ser = x.iloc[:,i].sort_values(ignore_index=True)                 #sorting values in descending order

            x_ser_new= pd.Series(x_ser)

    #             print(x_ser)
    #             print(len(x_ser))
    #             print(type(x_ser))
    #             print(x_ser.dtypes)
    #             print(x_ser.shape[0])


            if len(x_ser_new)% 2==0:                                       #checking length of data if 'even'

                res = (x_ser_new[len(x_ser_new)//2]+ x_ser_new[(len(x_ser_new)//2)-1])/2   # nth column and (n-1)th column average
                return res 

            else:                                                      # if length of data is 'odd'

                res = x_ser_new[len(x_ser_new)//2]                             # nth column 

                return res



construct_frame(df)

result should be for 10 columns but shows only 1st column median result:

1
34.26

答案1

得分: 1

不要在循环内部返回res,否则会在第一次迭代时中断循环:

def construct_frame(x):
    
    '''constructor function to get median of any data'''

    median = {}  # 此处
    if x.shape[1]>=0:
        for i in range(1,(x.shape[1])):
            x_ser = x.iloc[:,i].sort_values(ignore_index=True)
            x_ser_new = pd.Series(x_ser)

            if len(x_ser_new) % 2 == 0:
                res = (x_ser_new[len(x_ser_new) // 2] + x_ser_new[(len(x_ser_new) // 2) - 1]) / 2
            else:
                res = x_ser_new[len(x_ser_new) // 2]
            median[x_ser.name] = res  # 此处
    return pd.Series(median, name='median')  # 此处

输出:

>>> construct_frame(df)
Rank        2.0
Category      C
Name: median, dtype: object

更快的方法:

>>> df.iloc[:, 1:].quantile(0.5, numeric_only=False, interpolation='lower')
Rank        2.0
Category      C
Name: 0.5, dtype: object
英文:

Don't return res inside the loop otherwise it breaks the loop on the first iteration:

def construct_frame(x):
    
    '''constructor function to get median of any data'''

    median = {}  # HERE
    if x.shape[1]>=0:
        for i in range(1,(x.shape[1])):
            x_ser = x.iloc[:,i].sort_values(ignore_index=True)
            x_ser_new = pd.Series(x_ser)

            if len(x_ser_new)% 2==0:
                res = (x_ser_new[len(x_ser_new)//2]+ x_ser_new[(len(x_ser_new)//2)-1])/2
            else:
                res = x_ser_new[len(x_ser_new)//2]
            median[x_ser.name] = res  # HERE
    return pd.Series(median, name='median')  # HERE

Output:

>>> construct_frame(df)
Rank        2.0
Category      C
Name: median, dtype: object

A faster method:

>>> df.iloc[:, 1:].quantile(0.5, numeric_only=False, interpolation='lower')
Rank        2.0
Category      C
Name: 0.5, dtype: object

huangapple
  • 本文由 发表于 2023年5月30日 11:25:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定