Jua*_*n D 5 python dataframe pandas
我有这个数据框:
V1 V2
1 1
0 0
0 0
0 0
1 0
1 1
0 1
Run Code Online (Sandbox Code Playgroud)
我需要将每个V1和V2行值与名为comienzo的变量进行比较,并在该函数后面设置名为Label的第三列:
def labeling(DataFrame):
comienzo=0 #flag to indicate event started (1= started, 0= no started)
for n in range(len(DataFrame)):
if ((DataFrame.iloc[n]['V1']==1 & DataFrame.iloc[n]['V2']==1) & comienzo == 0 ) :
comienzo=1
DataFrame['Label']='Start'
elif ((DataFrame.iloc[n]['V1']==0 & DataFrame.iloc[n]['V2']==0) & comienzo == 1 ) :
comienzo=0
DataFrame['Label']='End'
return DataFrame
Run Code Online (Sandbox Code Playgroud)
我想使用 来做到这一点Dataframe.apply。所以,我尝试了这个:
def labeling(x, comienzo):
if ((x['V1']==1 & x['V2']==1) & comienzo == 0 ) :
comienzo=1
Label='Start'
elif ((x['V1']==0 & x['V2']==0) & comienzo == 1 ) :
comienzo=0
Label='End'
return Label
comienzo=0 #I initialize 'comienzo' var to 0
DataFrame['Label']=DataFrame.apply(labmda x: labeling(x,comienzo),axis=1)
Run Code Online (Sandbox Code Playgroud)
这项工作有效,但值不正确,我认为这.apply没有考虑变量comienzo。
是否有可能使该代码可编辑?
我想要这个输出:
comienzo=0
V1 V2
1 1 Start comienzo=1
0 1 NaN
0 0 End comienzo=0
0 0 NaN
1 0 NaN
1 1 Start comienzo=1
1 1 NaN
0 1 NaN
Run Code Online (Sandbox Code Playgroud)
你有一系列的小错误,从不正确的 lambda 使用(错误拼写 lambda),没有正确地使用 apply 与 args(如上所述),而且我很确定你打算在条件逻辑中使用 'and' 而不是 & 。
此外,您的输入数据是 7 行,而您的理想输出是 8 行,这使得尝试通过映射输入->输出来帮助解决问题在技术上是不可能的。
但是,我认为这就是您想要达到的目的:
DataFrame = pd.DataFrame(
[[1,1],
[0,1],
[0,0],
[0,0],
[1,0],
[1,1],
[0,1]])
DataFrame.columns=['V1','V2']
DataFrame.insert(0, 'comienzo', 0)
def labeling(x):
global comienzo
if ((x['V1']==1 and x['V2']==1) and comienzo == 0 ) :
comienzo=1
return('s')
elif ((x['V1']==0 and x['V2']==0) and comienzo == 1 ) :
comienzo=0
return('end')
comienzo=0
DataFrame['Label']=DataFrame.apply(labeling,axis=1)
Run Code Online (Sandbox Code Playgroud)
请注意,通过使用 comienzo 的全局变量,我们可以通过应用迭代来保留其值。
尽管在许多情况下使用全局变量是不好的做法。进一步阅读此处:为什么无法从 Python 中的 apply 函数内部访问其他变量?
| 归档时间: |
|
| 查看次数: |
9377 次 |
| 最近记录: |