Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在两列中同时执行逻辑运算_Python_Python 3.x_Pandas_Python 2.7_Sklearn Pandas - Fatal编程技术网

Python 在两列中同时执行逻辑运算

Python 在两列中同时执行逻辑运算,python,python-3.x,pandas,python-2.7,sklearn-pandas,Python,Python 3.x,Pandas,Python 2.7,Sklearn Pandas,我有一个数据帧名称df- id year month target1 0 324 2019.0 1.0 100.0 1 325 2019.0 3.0 100.0 2 326 2019.0 10.0 100.0 3 327 2019.0 11.0 100.0 4 328 2019.0 12.0 100.0 现在我想比较数据框的两列,即“年

我有一个数据帧名称df-

   id   year    month      target1  
0  324  2019.0  1.0        100.0    
1  325  2019.0  3.0        100.0   
2  326  2019.0  10.0       100.0    
3  327  2019.0  11.0       100.0    
4  328  2019.0  12.0       100.0   
现在我想比较数据框的两列,即“年”和“月”,以获得如下新数据框-

   id   year    month      amount  
0  324  2019.0  1.0        100.0    
1  325  2019.0  3.0        100.0   
2  326  2019.0  10.0       100.0    
df_new = df[df['year'] <= year ]
df_new = df_new[df_new['month_num'] <= month_num ]  
这里的行动是——

year <= 2019
&
month <= 10

谢谢

IIUC您希望应用两个条件作为df['condition1'和'condition2']。如果是这样,这可能会起作用:

df1 = df.loc[df['year'] <= 2019].loc[df['month'] <= 10]
df1的输出:

    id    year  month  target1
0  324  2019.0    1.0    100.0
1  325  2019.0    3.0    100.0
2  326  2019.0   10.0    100.0
如果我将本年度的条件更改为:

df1 = df.loc[df['year'] <= 2020].loc[df['month'] <= 10]

IIUC您希望应用两个条件作为df['condition1'和'condition2']。如果是这样,这可能会起作用:

df1 = df.loc[df['year'] <= 2019].loc[df['month'] <= 10]
df1的输出:

    id    year  month  target1
0  324  2019.0    1.0    100.0
1  325  2019.0    3.0    100.0
2  326  2019.0   10.0    100.0
如果我将本年度的条件更改为:

df1 = df.loc[df['year'] <= 2020].loc[df['month'] <= 10]

您可以尝试以下简单条件:

df['year'] = df['year'].astype(int)
df['month'] = df['month'].astype(int)

df = df[(df['year'] == 2019) & (df['month'] < 10)]

   id  year  month  target1
0  324  2019      1    100.0
1  325  2019      3    100.0
df['year']=df['year'].astype(int)
df['month']=df['month'].astype(int)
df=df[(df['年]=2019年)和(df['月]<10年)]
id年/月目标1
0  324  2019      1    100.0
1  325  2019      3    100.0

您可以尝试以下简单条件:

df['year'] = df['year'].astype(int)
df['month'] = df['month'].astype(int)

df = df[(df['year'] == 2019) & (df['month'] < 10)]

   id  year  month  target1
0  324  2019      1    100.0
1  325  2019      3    100.0
df['year']=df['year'].astype(int)
df['month']=df['month'].astype(int)
df=df[(df['年]=2019年)和(df['月]<10年)]
id年/月目标1
0  324  2019      1    100.0
1  325  2019      3    100.0

据我所知,您希望在指定年份的指定月份之前打印所有内容。这可以通过几种方式实现

  • 简单地加上

  • data.query('(年==2020,月据我所知,您希望在指定年份的指定月份之前打印所有内容。这可以通过多种方式完成

  • 简单地加上

  • data.query('(年份==2020和月份此逻辑与我尝试的相同,我没有从中获得期望的结果。什么不起作用?您是否在括号中添加了条件?此逻辑与我尝试的相同,我没有从中获得期望的结果。什么不起作用?您是否在括号中添加了条件?总之,
    loc
    应避免链接。Try this-df1=df.loc[df['year']一般来说,应该避免链接。试试这个-df1=df.loc[df['year']
    df['year'] = df['year'].astype(int)
    df['month'] = df['month'].astype(int)
    
    df = df[(df['year'] == 2019) & (df['month'] < 10)]
    
       id  year  month  target1
    0  324  2019      1    100.0
    1  325  2019      3    100.0
    
    data.query('(year == 2020 and month <= 1) or (year <= 2020)')