简体   繁体   English

根据另一列(Pandas)中的条件替换每行中的列中的内容

[英]Replacing contents in Columns in each row depending on condition in another column(Pandas)

I am new to Pandas.I want to replace the numbers in columns depending on month counter column in each row 我是Pandas的新手。我想根据每行中的月份计数器列替换列中的数字

CROSS (PUT X) ON THE COLUMN VALUES DEPENDING ON MONTH_COUNTER 在MONTH_COUNTER上的字符串值上的交叉(PUT X)

IF MONTH_COUNTER IS 1 DON'T CROSS ANY COLUMN: 如果MONTH_COUNTER是1,请勿交叉任何栏目:

2 CROSS DEC,JAN:
3 CROSS DEC,JAN,FEB:
4 CROSS DEC,JAN,FEB,MARCH:
5 CROSS DEC,JAN,FEB,MARCH,APRIL:
6 CROSS DEC,JAN,FEB,MARCH,APRIL

My Dataframe has millions of rows This is a sample (Binary Activity) 我的Dataframe有数百万行这是一个示例(二进制活动)

  DEC  JAN  FEB  MARCH  APRIL  MAY  Month_Counter
0    0    0    0      1      0    1              3
1    0    0    1      1      0    1              3
2    0    0    0      0      1    1              5
3    1    0    0      0      1    1              1
4    0    1    1      1      1    1              1
5    0    1    1      1      1    0              2
6    1    1    0      0      0    0              1
7    0    0    0      0      0    1              6
8    1    0    0      1      0    0              1
9    0    0    0      1      1    0              4

These are my two functions(Stike+Strike1) but they all cross all month when i call the function 这是我的两个功能(Stike + Strike1),但是当我调用该函数时它们都会在整个月内交叉

def Strike(df):
df['Month_Counter']= df['Month_Counter'].astype(int)
m=df['Month_Counter'].tolist()
n=[i for i,val in enumerate(m)]
k= pd.Series([m[i] for i in n])
s=(k.size)-1
for i in range(df.shape[0]):
    for j in range(0,s):
        if k[j]>1:
            df.iloc[:,0:k[j]]=df.replace(df.iloc[:,0:k[j]],'X')
return df

def Strike1(df):
Month_Counter = df['Month_Counter'].tolist()

for i in Month_Counter:
    if i > 1 :
       df=df.replace(df.iloc[:,0:i],'X')
return df
df1=Strike(df1)

print df1.head(20).to_string() print df1.head(20).to_string()

they produce he following results, please help,where do i go wrong?? 他们产生了以下结果,请帮忙,我哪里出错?

  DEC JAN FEB MARCH APRIL MAY  Month_Counter
0   X   X   X     X     X   X              3
1   X   X   X     X     X   X              3
2   X   X   X     X     X   X              5
3   X   X   X     X     X   X              1
4   X   X   X     X     X   X              1
5   X   X   X     X     X   X              2
6   X   X   X     X     X   X              1
7   X   X   X     X     X   X              6
8   X   X   X     X     X   X              1
9   X   X   X     X     X   X              4

This is the output I want 这是我想要的输出

   DEC  JAN  FEB  MARCH  APRIL  MAY  Month_Counter
0    X    X    X      1      0    1              3
1    X    X    X      1      0    1              3
2    X    X    X      X      X    1              5
3    1    0    0      0      1    1              1
4    0    1    1      1      1    1              1
5    X    X    1      1      1    0              2
6    1    1    0      0      0    0              1
7    X    X    X      X      X    X              6
8    1    0    0      1      0    0              1
9    X    X    X      X      1    0              4

The answer is here: 答案在这里:

for i in range(0,len(df.columns)):
    df.iloc[:,i] = np.where(df['Month_Counter'] >= i + 1, 'X', df.iloc[:,i])

Before: 之前:

   APR  DEC  FEB  JAN  MAR  MAY  Month_Counter
0    0    0    0    0    1    1              3
1    0    0    1    0    1    1              3
2    1    0    0    0    0    1              5
3    1    1    0    0    0    1              1
4    1    0    1    1    1    1              1
5    1    0    1    1    1    0              2
6    0    1    0    1    0    0              1
7    0    0    0    0    0    1              6
8    0    1    0    0    1    0              1
9    1    0    0    0    1    0              4

After: 后:

  DEC FEB JAN MAR MAY      Month_Counter
0   X   X   X   0   1   1              3
1   X   X   X   0   1   1              3
2   X   X   X   X   X   1              5
3   1   1   0   0   0   1              1
4   1   0   1   1   1   1              1
5   X   X   1   1   1   0              2
6   0   1   0   1   0   0              1
7   X   X   X   X   X   X              6
8   0   1   0   0   1   0              1
9   X   X   X   X   1   0              4

I think this should do it 我认为应该这样做

df1 = pd.DataFrame({'DEC':[0,1,0,0,0,1,1,0,0,1],'JAN':[0,1,1,0,0,1,1,1,0,0],'FEB':[0,1,0,0,1,1,1,1,0,1],'MAR':[0,1,0,0,1,1,1,1,0,1],"Month_Counter":[3,3,2,1,1,2,1,2,1,3]})


for i in range(len(df1["Month_Counter"])):
    for j in range(df1.loc[i, "Month_Counter"]):
        if int(df1.loc[i,"Month_Counter"]) > 1:
            df1.iloc[i,j]=str(df1.iloc[i,j])
            df1.iloc[i, j]="X"

 DEC FEB JAN  MAR  Month_Counter
0   X   X   X    0              3
1   X   X   X    1              3
2   X   X   1    0              2
3   0   0   0    0              1
4   0   1   0    1              1
5   X   X   1    1              2
6   1   1   1    1              1
7   X   X   1    1              2
8   0   0   0    0              1
9   X   X   X    1              3

Thank you all this also worked for me 谢谢大家这对我也有用

def Strike(df):
df['Month_Counter']= df['Month_Counter'].astype(int)
m=df['Month_Counter'].tolist()
n=[i for i,val in enumerate(m)]
k= pd.Series([m[i] for i in n])
for i in range(df.shape[0]):
    if k[i]>1:
        df.loc[[i],0:k[i]]=df.replace(df.iloc[:,0:k[i]],'X')
return df

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

相关问题 熊猫:根据条件为每个组添加行 - Pandas: add row to each group depending on condition 熊猫:根据索引将每一行除以另一行 - Pandas: divide each row by another row depending on the index 删除重复的列值并根据 pandas 中的条件选择保留该行 - Remove duplicated column values and choose to keep the row depending on condition in pandas 具有累积值的新 Pandas 列取决于前一行的条件 - New Pandas column with cumulative value depending on condition on the previous row 我希望添加一列并根据条件在每一行中更新它 - I wish to add a column and update it in each row depending on a condition 根据条件用另一列值替换大量列中的值 - Replacing values in large number of columns with another column value based on a condition pandas - 根据列值复制每行'n'次 - pandas - Copy each row 'n' times depending on column value 熊猫-将所有列中的特定值替换为另一列中的对应值 - Pandas - Replacing a specific value in all columns with the corresponding value in another column 对于df中的每一行,将N-1列保存到新行中,将第N列保存到另一行中(熊猫) - Save, for each row in df, N-1 columns into a new line and the N-th column into another line (pandas) 如何根据另一列对一行中的特定列求和? - How can I sum specific columns in a row, depending on another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM