小编use*_*756的帖子

查找 Pandas DataFrame 中的日期是周末还是工作日

我有一个数据框 df,有 3 列:年、月和日期。我想使用此信息来确定该日期是周末还是工作日以及是一周中的哪一天。

我试图使用下面的代码来确定是周末还是工作日,但它不起作用

from datetime import datetime
from datetime import date

def is_weekend(d = datetime.today()):
  return d.weekday() > 4
Run Code Online (Sandbox Code Playgroud)
df['weekend'] = df.apply(lambda x: is_weekend(date(x['Year'], x['Month'], x['Day'])), axis=1)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

TypeError: cannot convert the series to <class 'int'>
Run Code Online (Sandbox Code Playgroud)

除此之外,我如何知道这个日期是否是一周中的哪一天。

python datetime date dataframe pandas

11
推荐指数
2
解决办法
2万
查看次数

在 Python 中将字典转换为 JSON

我需要帮助将以下字典转换为 json:

summary = {'model': 'Test', 'devices': '0,1', 'config': {'data': {'name': 'ABC', 'labels': 'outputs/export2_v2', 'cache_in_memory': False, 'validation_size': 192, 'augmentation': {'photometric': {'enable': True, 'primitives': ['random_brightness', 'random_contrast', 'additive_speckle_noise', 'additive_gaussian_noise', 'additive_shade', 'motion_blur'], 'params': {'random_brightness': {'max_abs_change': 50}, 'random_contrast': {'strength_range': [0.3, 1.5]}, 'additive_gaussian_noise': {'stddev_range': [0, 10]}, 'additive_speckle_noise': {'prob_range': [0, 0.0035]}, 'additive_shade': {'transparency_range': [-0.5, 0.5], 'kernel_size_range': [100, 150]}, 'motion_blur': {'max_kernel_size': 3}}}, 'homographic': {'enable': True, 'params': {'translation': True, 'rotation': True, 'scaling': True, 'perspective': True, 'scaling_amplitude': 0.2, 'perspective_amplitude_x': 0.2, 'perspective_amplitude_y': 0.2, 'patch_ratio': 0.85, 'max_angle': 1.57, 'allow_artifacts': True}, 'valid_border_margin': …
Run Code Online (Sandbox Code Playgroud)

python json dictionary python-3.x

5
推荐指数
1
解决办法
5066
查看次数

pandas Dataframe 中仅某些列的总和

我有一个类似于下面的数据框。我只需要添加某些列的总和:16 年 1 月、16 年 2 月、16 年 3 月、16 年 4 月和 16 年 5 月。我将这些列放在名为 Months_list 的列表中

--------------------------------------------------------------------------------------
| Id               |  Name             | Jan-16 | Feb-16 | Mar-16 | Apr-16 | May-16   |
| 4674393          |  John Miller      |  0     | 1      | 1      | 1      | 1        |
| 4674395          |  Joe Smith        |  0     | 0      | 1      | 1      | 1        |
---------------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我的输出应如下所示:

--------------------------------------------------------------------------------------
| Id               |  Name             | Jan-16 | Feb-16 | Mar-16 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

标签 统计

python ×3

dataframe ×2

pandas ×2

date ×1

datetime ×1

dictionary ×1

json ×1

python-3.x ×1