zer*_*dge 4 python sqlalchemy pandas
当使用 Pandasread_sql使用 SQLAlchemy 查询我的数据库时,我收到以下警告:
SAWarning:表上的列 'id' 被具有相同键的 Column('id', Integer(), table=, primary_key=True, nullable=False) 替换。考虑 use_labels 用于 select() 语句。(key, getattr(existing, 'table', None), value))
对,所以我的每一个League,Season,Round,Match,和Team表有一个名为列id。当然。
一开始我忽略了这一点,但是当我想id使用pd.drop(). 甚至pd.rename通过索引 (!) 引用该列,重命名所有具有相同名称的列:
pandoc.rename(
columns={pandoc.columns[1]: 'match_id'},
inplace=True)
# This replaced all columns with the label `id` to `match_id`
Run Code Online (Sandbox Code Playgroud)
SQLAlchemy 建议我将考虑 use_labels 用于 select() 语句,虽然我设法处理常规查询,但我无法真正弄清楚.label('new_column_name')在以下查询中的位置:
pandoc = pd.read_sql(
Match.query.options(
joinedload(Match.home_team),
joinedload(Match.away_team)).statement,
db.session.bind,
parse_dates=['date_time'])
Run Code Online (Sandbox Code Playgroud)
一种选择是将id表中的所有列更改为tablename_id,但这对于应该具有相当简单解决方案的问题来说似乎是一种丑陋的解决方法。
示例输出print(pandoc.head()):
total_goals id round_id \
0 1.0 somestring here s12786-0
1 0.0 somestring here s12786-0
2 5.0 somestring here s12786-0
3 3.0 somestring here s12786-0
4 0.0 somestring here s12786-0
home_team_id away_team_id id id
0 667 664 667 664
1 669 691 669 691
2 672 677 672 677
3 707 686 707 686
4 699 703 699 703
Run Code Online (Sandbox Code Playgroud)
注意3id列,一是比赛ID,另外两列是主队ID和客队ID。
| 归档时间: |
|
| 查看次数: |
1200 次 |
| 最近记录: |