假设我们有以下 df
import pandas as pd
data = {'Dates' : ['2018-10-15', '2018-02-01', '2018-04-01']}
data['Dates'] = pd.to_datetime(data.Dates)
print(df)
Dates
0 2018-10-15
1 2018-02-01
2 2018-04-01
Run Code Online (Sandbox Code Playgroud)
在我现在的公司,我们有一个财务周结构,我通常使用 Excel 来计算,我想用 Python 来实现
我使用 DateTime 模块来解决我的条件,如下所示
如果月份是>= 4(四月),则周数为 1(因此我采用 ISO 周数并减去 13)
如果月份是< 4我添加 39。
我对 YEAR 使用相同的逻辑 if >= 4 then Year + 1 else YEAR
我想我可以使用一个简单的 for 循环,我可以在我的数据帧上使用它
for x in data.Dates:
if x.dt.month >= 4:
df['Week'] = x.dt.week - 13
else:
df['Week'] = x.dt.week + 39
Run Code Online (Sandbox Code Playgroud)
以及今年 …
两个数据框
例如。数据1
id : [1, 2, 3]
value: [2, 3, 5]
Run Code Online (Sandbox Code Playgroud)
数据2
id : [1, 2, 4]
value: [1, 3, 5]
Run Code Online (Sandbox Code Playgroud)
寻找这个输出:
id : [1, 2, 3, 4]
value: [3, 6, 5, 5]
Run Code Online (Sandbox Code Playgroud) 我有一个 CSV 文件,其中包含有关驾车旅行的信息。
我想整理这些数据,以便为每个旅程(每一行)创建一个列表。该列表应包含 travel_code 作为列表中的第一项,然后将所有后续 MGRS 单元作为单独的项。最后,我希望将所有这些旅程列表分组到一个父列表中。
如果我手动执行此操作,它将如下所示:
journeyCodeA = ['journeyCodeA', 'mgrs1', 'mgrs2', 'mgrs3']
journeyCodeB = ['journeyCodeB', 'mgrs2', 'mgrs4', 'mgrs7']
combinedList = [journeyCodeA, journeyCodeB]
Run Code Online (Sandbox Code Playgroud)
这是我迄今为止为每行创建一个列表并组合所需列的内容。
comparison_journey_mgrs = pd.read_csv(r"journey-mgrs.csv", delimiter = ',')
comparison_journey_mgrs['mgrs_grids'] = comparison_journey_mgrs['mgrs_grids'].str.replace(" ","")
comparison_journey_list = []
for index, rows in comparison_route_mgrs.iterrows():
holding_list = [rows.journey_code, rows.mgrs_grids]
comparison_journey_list.append(holding_list)
Run Code Online (Sandbox Code Playgroud)
问题在于它将 mgrs_grids 列视为单个字符串。
我的清单是这样的:
[['7211863-140','18TWL927129,18TWL888113,18TWL888113,...,18TWL903128']]
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
[['7211863-140','18TWL927129', '18TWL888113', '18TWL888113',..., '18TWL903128']]
Run Code Online (Sandbox Code Playgroud)
我正在努力寻找一种方法来遍历数据框的每一行,引用 mgrs_grids 列,然后将逗号分隔的字符串转换为就地列表。
谢谢你的帮助!
{'driver_code': {0: 7211863, 1: 7211863, 2: 7211863, 3: 7211863},
'journey_code': {0: '7211863-140',
1: '7211863-105',
2: '7211863-50',
3: …Run Code Online (Sandbox Code Playgroud) 我有一个简单的日期维度,
SELECT [actualDate]
FROM (
VALUES ('2021-09-20')
, ('2021-09-21')
, ('2021-09-22')
, ('2021-09-23')
, ('2021-09-24')
, ('2021-09-25')
, ('2021-09-26')
, ('2021-09-27')
, ('2021-09-28')
) as t(actualDate)
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个窗口函数(没有太多运气......)来获得以下结果。
其中 1 是今天,0 是昨天。
+----------+----------+
|actualDate|dateOffset|
+----------+----------+
|2021-09-20|-4 |
|2021-09-21|-3 |
|2021-09-22|-2 |
|2021-09-23|-1 |
|2021-09-24|0 |
|2021-09-25|1 |
|2021-09-26|2 |
|2021-09-27|3 |
|2021-09-28|4 |
+----------+----------+
Run Code Online (Sandbox Code Playgroud)
接下来我可以尝试什么?我不确定要搜索什么。