小编Tra*_*VOX的帖子

将 Pandas DataFrame 行合并为一列中的字符串

鉴于此数据帧

       r3  value
r1 r2           
1  2    3      1
   2    4      1
   3    2      1
   3    4      1
   4    2      1
   4    3      1
2  1    3      1
   1    4      1
   3    1      1
   3    4      1
   4    1      1
   4    3      1
Run Code Online (Sandbox Code Playgroud)

......这样做的最佳方法是什么?

        r3     value
r1 r2           
1  2    3,4    2
   3    2,4    2
   4    2,3    2
2  1    3,4    2
   3    1,4    2
   4    1,3    2
Run Code Online (Sandbox Code Playgroud)

基本上,我试图将r3列压缩为逗号分隔的字符串。value如果有必要,可以稍后以不同的方式实现该列,或者如果可以通过整个过程完成,则更好。

python pandas

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

冲突时,在具有非空约束的 Postgres 中不执行任何操作

是否不可能利用“ON CONFLICT DO NOTHING”来避免插入和违反非空约束?

例如...

INSERT INTO public.users (user, user_yob, sex) 
SELECT mom, mom_yob, 'F' 
FROM staging.users 
ON CONFLICT DO NOTHING;
Run Code Online (Sandbox Code Playgroud)

产生此错误并停止...

INSERT INTO public.users (user, user_yob, sex) SELECT mom, mom_yob, 'F' FROM staging.users ON CONFLICT DO NOTHING
> ERROR:  null value in column "user" violates not-null constraint
  DETAIL:  Failing row contains (0b159b81-6842-4ae7-961c-2e9cff8488b1, null, null, null, null, null, null, null, null, null, F).
Run Code Online (Sandbox Code Playgroud)

理想情况下,这个特定的插入将被忽略而不是引发错误。

postgresql

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

How would you unit test this SQLAlchemy Core query/function?

I'm working on learning how to unit test properly. Given this function...

def get_user_details(req_user_id):
    users = sa.Table('users', db.metadata, autoload=True)

    s = sa.select([users.c.username,
                   users.c.favorite_color,
                   users.c.favorite_book
                   ]).select_from(users)

    s = s.where(users.c.user_id == req_user_id)

    result = db.connection.execute(s).fetchone()
    return dict(result)
Run Code Online (Sandbox Code Playgroud)

...what is the proper way to unit test it?

Here's where I am now...

  1. From what I've read, testing the "construct" of the query is unnecessary as that's part of the already well-tested SQLAlchemy library. So I don't need to test the raw SQL generated, …

python sqlalchemy pytest

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

Python 枚举属性可以有一个空字符串吗?

我真的很想在我正在研究的这个模型中使用 Python 枚举类型。问题是来自数据提供者的潜在值之一是空字符串。

鉴于这个模型...

from sqlalchemy import Column, Enum

class Events(Base):
        __tablename__ = 'events'
        ...
        restriction_code = Column(Enum(RestrictionCode))
        ...
Run Code Online (Sandbox Code Playgroud)

...其中一个枚举类型将有一个“空字符串”作为这样的属性...

from enum import Enum

class RestrictionCode(Enum):
    A = 'A Description'
    B = 'B Description'
    C = 'C Description'
    D = 'D Description'
    '' = 'No Restriction'
Run Code Online (Sandbox Code Playgroud)

上面的结果并不奇怪:SyntaxError: can't assign to literal。这是否可能或者有解决方法吗?

python enums sqlalchemy

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

使用其他列有条件地更新Pandas DataFrame列

使用如下所示的DataFrame,当c1pos等于零时,如何将c1len设置为零?我想为c2len/c2pos做同样的事情.是否有一种简单的方法可以在不创建一堆列的情况下实现所需的答案?

             distance  c1pos  c1len  c2pos  c2len  daysago
 line_date                                                
 2013-06-22      7.00      9    0.0      9    6.4       27
 2013-05-18      8.50      6    4.6      7    4.9       62
 2012-12-31      8.32      5    4.6      5    2.1      200
 2012-12-01      8.00      7    7.1      6    8.6      230
 2012-11-03      7.00      7    0.0      7    2.7      258
 2012-10-15      7.00      7    0.0      8    5.2      277
 2012-09-22      8.32     10   10.1      8    4.1      300
 2012-09-15      9.00     10   12.5      9   12.1      307
 2012-08-18      7.00      8    0.0      8    9.2      335
 2012-08-02      9.00      5    3.5      5    2.2      351
 2012-07-14     12.00 …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

如何在 Python 中复制 Excel 的“Power Trendline”?

如何在 Python 中重新创建“Excel Power Trendline”并获取系数?

在 Excel 中,这些数据...

x = [5.5, 6.0, 6.5, 7, 9]
y = [64.0575, 69.656, 75.781, 82.7023, 111.156866]
Run Code Online (Sandbox Code Playgroud)

...创建产生回归公式的趋势线:

y = 9.2347 * (x ^ 1.1294)
Run Code Online (Sandbox Code Playgroud)

我想在 Python 中执行此操作,以便稍后在我的软件中利用公式中的系数。

谢谢!

python

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

使用 Python 在 AWS Lambda 上出现前导斜杠问题的 FileNotFoundError

我的开发环境是一台 Windows 机器。当尝试从本地 S3 下载文件时,它没有问题。但是,当我将函数加载到 Lambda 时,我收到一个FileNotFoundError错误,这是由 Lambda 要求文件键中的前导斜杠引起的。

这在本地有效,但在 Lambda 上无效...

s3 = boto3.resource('s3')
new_file_key = os.path.join('tmp', file_name)
s3.Bucket('bucketname').download_file(file_key, new_file_key)
Run Code Online (Sandbox Code Playgroud)

这适用于 Lambda,但不适用于本地...

s3 = boto3.resource('s3')
new_file_key = os.path.join('/tmp', file_name)
s3.Bucket('bucketname').download_file(file_key, new_file_key)
Run Code Online (Sandbox Code Playgroud)

处理这个问题的最简单方法是什么?

python amazon-s3 aws-lambda

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

将DataFrame行值合并为逐个字符串

如果我有以下DataFrame ...

           code
player_id      
223336        4
223336        5
223336        4
225987        2
225987        3
225987        4
Run Code Online (Sandbox Code Playgroud)

如何将"代码"列合并为一个字符串,以便结果看起来像......

             code
player_id      
223336        454
225987        234
Run Code Online (Sandbox Code Playgroud)

谢谢!

python pandas

0
推荐指数
1
解决办法
331
查看次数

标签 统计

python ×7

pandas ×3

sqlalchemy ×2

amazon-s3 ×1

aws-lambda ×1

enums ×1

postgresql ×1

pytest ×1