[英]Can i substract 2 column given some conditions pandas
I have a df that looks like this我有一个看起来像这样的 df
I want to subtract EndTime - StartTime
only if channel
is 1000 smth like this: df.loc[df['Channel'] == 1000]['X channel view time']=df['EndTime']-df['StartTime']
this should be the fastest but does not appear to be working so我想减去
EndTime - StartTime
仅当channel
是 1000 smth 时才这样: df.loc[df['Channel'] == 1000]['X channel view time']=df['EndTime']-df['StartTime']
这应该是最快的,但似乎不起作用
def watch_time(row,channel):
val=0
if row['Channel']==channel:
val=row['EndTime']-row['StartTime']
return val
df['BTV_view_time'] = df.apply(watch_time,args=250,axis=1)
but this is a lot slower?但这慢了很多?
You can do it for all rows then replace ones that don't fit your filter with 0
:您可以对所有行执行此操作,然后用
0
替换不适合您的过滤器的行:
df['X channel view time'] = df['EndTime'] - df['StartTime']
df.loc[df['Channel'] == 1000, 'X channel view time'] = 0
You were not that far.你没有那么远。 Syntax is:
语法是:
df.loc[df['Channel'] == 1000, 'X channel view time']=df.loc[df['Channel'] == 1000, 'EndTime']-df.loc[df['Channel'] == 1000, 'StartTime']
You could try something like this:你可以尝试这样的事情:
import numpy as np
df['X channel view time'] = np.where(df['channel'] == 1000, df['EndTime'] - df['StartTime'], 0)
notice you are taking a slice of the df, that's why it is not working, you can do something like this:请注意,您正在获取 df 的一部分,这就是它不起作用的原因,您可以执行以下操作:
smaller_df = df[df['Channel'] == 1000]['EndTime']-df[df['Channel'] == 1000]['StartTime']
you can also create a new column:您还可以创建一个新列:
df['subtraction'] = [0]*len(df)
df['subtraction'] = df[df['Channel'] == 1000]['EndTime']-df[df['Channel'] == 1000]['StartTime']
replace [0]*len(df)
by what you want if 'Channel' is different of 1000如果 'Channel' 与 1000 不同,则将
[0]*len(df)
替换为您想要的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.