小编ABY*_*ABY的帖子

python如何查找从2019年12月开始的每个月的天数,并在两个日期列之间向前推进

我有两个日期列“StartDate”和“EndDate”。我想找到从 2019 年 12 月开始的这两个日期之间每个月的天数,并忽略 2019 年之前的任何月份进行计算。每行的 StartDate 和 EndDate 可以跨越 2 年,月份重叠,日期列也可以为空。

样本数据:

df = {'Id': ['1','2','3','4','5','6','7', '8'],
      'Item': ['A','B','C','D','E','F','G', 'H'],
        'StartDate': ['2019-12-10', '2019-12-01', '2019-10-01', '2020-01-01', '2019-03-01','2019-03-01','2019-10-01', ''],
        'EndDate': ['2020-02-21' ,'2020-01-01','2020-08-31','2020-01-30','2019-12-31','2019-12-31','2020-08-31', '']
        }
df = pd.DataFrame(df,columns= ['Id', 'Item','StartDate','EndDate'])
Run Code Online (Sandbox Code Playgroud)

预期 O/P:

1

以下解决方案部分有效。

df['StartDate'] = pd.to_datetime(df['StartDate'])
df['EndDate'] = pd.to_datetime(df['EndDate'])

def days_of_month(x):
    s = pd.date_range(*x, freq='D').to_series()
    return s.resample('M').count().rename(lambda x: x.month)

df1 = df[['StartDate', 'EndDate']].apply(days_of_month, axis=1).fillna(0)

df_final = df[['StartDate', 'EndDate']].join([df['StartDate'].dt.year.rename('Year'), df1])
Run Code Online (Sandbox Code Playgroud)

python numpy dataframe pandas

7
推荐指数
1
解决办法
114
查看次数

Bigquery Full Join ON 多个条件

我想对具有多个条件的两个表执行完全外连接,以生成两个表中的所有匹配记录以及不匹配的记录。Tbl1 是一个更大的表,有 21 M 条记录,Tbl2 有 5k 行,如下面的示例查询。但外连接无法使用 OR 条件执行,因为出现错误“如果没有连接两侧字段相等的条件,则无法使用 FULL OUTER JOIN”。在这种情况下,编写单独的查询,然后使用 COALESCE 是唯一的解决方案吗?我不确定如何实施这个解决方案。寻求任何帮助来解决这个问题。

SELECT  
* 
FROM 
`myproject.table1` as t1
Full Outer JOIN
`myproject.table2` as t2
ON
(
t1.Camp1ID = t2.ID
OR t1.Camp2ID = t2.ID
OR t1.Camp3ID = t2.ID
OR t1.Camp4ID = t2.ID
OR t1.Camp5ID h = t2.ID
OR t1.Camp6ID = t2.ID
OR t1.Camp7ID = t2.ID
OR t1.Camp8ID = t2.ID
OR t1.Camp9ID = t2.ID
OR t1.Camp10ID = t2.ID
OR t1.Camp11ID = t2.ID
OR t1.Camp12ID = t2.ID
OR t1.Camp13ID = …
Run Code Online (Sandbox Code Playgroud)

sql join outer-join google-bigquery

3
推荐指数
1
解决办法
2910
查看次数

标签 统计

dataframe ×1

google-bigquery ×1

join ×1

numpy ×1

outer-join ×1

pandas ×1

python ×1

sql ×1