繁体   English   中英

Python:如何在每一列中为每行中的给定总数分配值

[英]Python: How to assign value in each column with a given total in each row

我想为超过 30 个新列的每一行分配特定于行的值。 我有一个名为 totalnumber(Int) 的列,我想创建 30 个新列并将值 1 分配给每个新列重复,直到 30 列的总和等于总数的值。 像这样

Total Number col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 so on col30
9            1    1    1    1    1    1    1    1    1    0     0     0           0
30           1    1    1    1    1    1    1    1    1    1     1     1           1
35           2    2    2    2    2    1    1    1    1    1     1     1           1

我是 python 的新手,我想我需要一段时间和 for 循环,但现在知道如何 go 即将执行此操作。 任何人都可以帮忙吗?

我只能想到使用下面的代码将部分分配到第一列,但这不是我想要的......

df = baseline.loc[baseline.Pathway == "Referred", grouping_cols + ["TotalNumbers"]] 
for col in list(range(1, 31)): #Iterate through the 30 columns
    referred[col] = np.floor(df["TotalNumbers"] / 30) 
df[1] = df[1] + (df["TotalNumbers"] % 30) 


import pandas as pd

df = pd.DataFrame({"Total Number": [9, 30, 35]})



# define which columns need to be created
# this will be the range between 1 and the maximum of the Total Number column
columns_to_fill = ["col" + str(i) for i in range(1, 31)]
# columns_to_fill = [ col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, .... , col35 ]


# now, go through each row of your dataframe
for indx, row in df.iterrows():
    # and for each column in the new columns to be filled
    # check if the number is smaller or equal than the row's Total Number
    # if it is smaller, fill the column with 1
    # else fill the column with 0
    
    for number, column in enumerate(columns_to_fill):
        if number + 1 <= row["Total Number"]:
            df.loc[indx, column] = 1
        else:
            df.loc[indx, column] = 0
    

    # now check if there is a remainder
    remainder = row["Total Number"] - 30
    
    # while remainder is greater than 0
    # we need to continue adding +1 to the columns
    while remainder > 0:
        for number, column in enumerate(columns_to_fill):
            if number + 1 <= remainder:
                df.loc[indx, column] += 1
            else:
                continue
        # update remainder
        remainder = remainder - 30


print(df)

在 pandas 中,可以使用二维切片(如在 NumPy 中),到 select 的一部分 dataframe。 这在这里非常方便,因为它允许您一次将1分配给一行的切片。

首先,我将保存 dataframe 中现有列的数量,以便您可以轻松地从中向上计数。 然后将新列分配为零。

在这些准备工作之后,您可以遍历行并将1分配给每行的切片,其长度由Total Number给出:

import pandas as pd

df = pd.DataFrame({'Total Number': [9, 30, 35]})
n_columns = len(df.columns)

for newcol in range(1, 31):
    df['col' + str(newcol)] = 0
    
for row in range(len(df)):
    total = df['Total Number'][row]
    df.iloc[row, n_columns : n_columns + total] = 1

要在总数超过 30 的那些行中获得2 s,您可以在将每个总数减少 30 后重复该过程:

remainder = df['Total Number'] - 30

for row in range(len(df)):
    if remainder[row] > 0:
        df.iloc[row, n_columns : n_columns + remainder[row]] += 1

如果总数可以大于 60,您可能需要编写一个额外的循环来重复此过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM