从SQLAlchemy Join处理Pandas DataFrame构造函数中的重复列 [英] Handling Duplicate Columns in Pandas DataFrame constructor from SQLAlchemy Join
本文介绍了从SQLAlchemy Join处理Pandas DataFrame构造函数中的重复列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道read_csv
具有mangle_dup_cols
,但是发出后如何在sqlalchemy中通过sql join执行同样的操作:
I know that read_csv
has mangle_dup_cols
but how can I do the same from a sql join in sqlalchemy after issuing:
pd.DataFrame(result.fetchall(), columns=result.keys())
在使用df.info()
时出现错误,因为列名是重复的.
which gives me an error when using df.info()
because of dupe col names.
推荐答案
您可以创建自己的帮助程序功能,该功能可以处理列名.我从 io.parsers._infer_columns
:
You can create your own helper function which mangles column names. The code below I copied from the io.parsers._infer_columns
:
def mangle_dupe_cols(columns):
counts = {}
for i, col in enumerate(columns):
cur_count = counts.get(col, 0)
if cur_count > 0:
columns[i] = '%s.%d' % (col, cur_count)
counts[col] = cur_count + 1
return columns
pd.DataFrame(result.fetchall(), columns=mangle_dupe_cols(result.keys()))
这篇关于从SQLAlchemy Join处理Pandas DataFrame构造函数中的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文