简体   繁体   English

根据每行的计算条件设置列值

[英]Set Column Value Based on Calculate Condition from Each Row

I have a empty dataframe as我有一个空的 dataframe 作为

columns_name = list(str(i) for i in range(10))
dfa = pd.DataFrame(columns=columns_name, index=['A', 'B', 'C', 'D'])
dfa['Count'] = [10, 6, 9, 4]
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 Count数数
A一个 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10 10
B NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 6 6
C C NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 9 9
D D NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 4 4

I want to replace Nan values with a symbol with the difference of max(Count) - Current(max) .我想用具有max(Count) - Current(max)差异的符号替换Nan值。 So, the final result will look like.所以,最终的结果会是这样的。

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 Count数数
A一个 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 10 10
B NaN NaN NaN NaN NaN NaN - - - - - - - - 6 6
C C NaN NaN NaN NaN NaN NaN NaN NaN NaN - - 9 9
D D NaN NaN NaN NaN - - - - - - - - - - - - 4 4

I am stuck at我被困在

dfa.at[dfa.index, [str(col) for col in list(range(dfa['Count'].max() - dfa['Count']))]] = '-'

and getting KeyError: 'Count'并得到KeyError: 'Count'

Actually, your this part of the code dfa.at[dfa.index, [str(col) for col in list(range(dfa['Count'].max() - dfa['Count']))]] = '-' has issue.实际上,您的这部分代码dfa.at[dfa.index, [str(col) for col in list(range(dfa['Count'].max() - dfa['Count']))]] = '-'有问题。

Just try to create the list which you are trying to use inside comprehension只需尝试创建您尝试在理解中使用的列表

list(range(dfa['Count'].max() - dfa['Count']))

It'll throw TypeError它会抛出TypeError

If you notice, you'll figure out that (dfa['Count'].max() - dfa['Count']) will give following series :如果您注意到,您会发现(dfa['Count'].max() - dfa['Count'])将给出以下series

A    0
B    4
C    1
D    6

And since you're trying to pass a series to python's range function, it will throw the error.而且由于您试图将series传递给python的range function,它会抛出错误。

One possible solution might be:一种可能的解决方案可能是:

for index, cols in zip(dfa.index, [list(map(str, col)) for col in (dfa).apply(lambda x: list(range(x['Count'], dfa['Count'].max())), axis=1).values]):
    dfa.loc[index, cols] = '-'

OUTPUT : OUTPUT

Out[315]: 
     0    1    2    3    4    5    6    7    8    9  Count
A  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN     10
B  NaN  NaN  NaN  NaN  NaN  NaN    -    -    -    -      6
C  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN    -      9
D  NaN  NaN  NaN  NaN    -    -    -    -    -    -      4

Broadcasting is also an option:广播也是一种选择:

import pandas as pd
import numpy as np

columns_name = list(str(i) for i in range(10))
dfa = pd.DataFrame(columns=columns_name, index=['A', 'B', 'C', 'D'])
dfa['Count'] = [10, 6, 9, 4]

# Broadcast based on column index (Excluding Count)
m = (
        dfa['Count'].to_numpy()[:, None] == np.arange(0, dfa.shape[1] - 1)
).cumsum(axis=1).astype(bool)
# Grab Columns To Update
non_count_columns = dfa.columns[dfa.columns != 'Count']
# Update based on mask
dfa[non_count_columns] = dfa[non_count_columns].mask(m, '-')

print(dfa)

Output: Output:

     0    1    2    3    4    5    6    7    8    9  Count
A  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN     10
B  NaN  NaN  NaN  NaN  NaN  NaN    -    -    -    -      6
C  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN    -      9
D  NaN  NaN  NaN  NaN    -    -    -    -    -    -      4

暂无
暂无

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

相关问题 for 循环,用于根据 Python 中的条件将每一行的值与另一个表中的特定列相乘 - for Loop for multiplying a value of each row with a specific column from another table based on condition in Python 根据行值的条件从数据框中提取列名 - Extracting column names from a dataframe based on condition on row value 根据对列中每个不同值具有特定条件的行计算 Pandas Dataframe 中的 perc - Calculate perc in Pandas Dataframe based on rows having a specific condition for each distinct value in column 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据具有条件的其他列设置列的值 - Set value of a column based on other column with an condition 根据“ B”列的条件,从“ A”列计算“ NEW COLUMN” - Calculate 'NEW COLUMN' from column 'A' based on condition from column 'B" 根据条件识别具有行中第一个值的列 - Identifying column with the first value in the row based on a condition 根据python中的条件使用第1行或第2行的值更新数据框列 - Update dataframe column based with value from either row 1 or row 2 based on condition in python 如何为每一行逐行计算列值的顺序? - How to calculate for each row the order of column value by row? Python pandas 确保基于列值的每一行都有一组数据存在,如果不添加行 - Python pandas to ensure each row based on column value has a set of data present, if not add row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM