简体   繁体   English

在某些条件下,我可以减去 2 列熊猫吗

[英]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.

相关问题 Pandas:如何从列中重复减去数组 - Pandas: How can I substract an array from a column repeatedely pandas dataframe 中的 2 列在某些给定条件下的差异 - Difference of 2 columns in pandas dataframe with some given conditions Pandas / Pyspark for 循环列减法 - Pandas / Pyspark for loop column substract 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 用熊猫在一列中减去每两行 - substract each two row in one column with pandas 根据其他列中的条件替换pandas列中的某些特定值 - Replace some specific values in pandas column based on conditions in other column 如何使用 groupby 在给定一些预定义条件的情况下确定字段的最后一个值? - How can I determine the last value of a field given some predefined conditions using groupby? 在 DataFrame pandas 中执行具有给定条件的表的列更新 - Perform column update of a table with given conditions in a DataFrame pandas 如何根据一组条件在 PANDAS 中创建一个新列,然后将新列设置为另一个字段的值 - How can I create a new column in PANDAS based on a set of conditions and then setting the new column to the value of another field 如何使用 Pandas 根据某些条件或函数匹配来自不同数据帧的值? - How can I match values from different dataframes based on some conditions or function using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM