[英]How to get the percentage of each value in a row basis row total in 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.