我是Pandas的新手。我想根据每行中的月份计数器列替换列中的数字
关于MONTH_COUNTER的栏值的交叉(PUT X)
如果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有数百万行这是一个示例(二元活动)
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
这是我的两个功能(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()
他们产生了以下结果,请帮助,我哪里出错?
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
这是我想要的输出
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
答案 0 :(得分:1)
我认为应该这样做
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
答案 1 :(得分:1)
答案就在这里:
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
<强>之前:强>
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
<强>后:强>
Date Time Global_active_power
16/12/2006 17:24:00 4.216
16/12/2006 18:25:00 4.5
17/12/2006 17:25:00 4.52
18/12/2006 17:25:00 4.557
答案 2 :(得分:0)
谢谢你们这一切对我有用
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