我正在网站上阅读SQL和Pandas的比较 possible。在这里,当我执行sql内部联接时,列的名称会重复。如何给他们起适当的名字?
Id_user Login: {{userdata[0].parent_id}}
<ion-item *ngFor="let itemData of item ">
<ion-row >
<ion-col>
<div>
<span>Id_user: {{itemData.studentid}}</span><br>
<span>{{itemData.title}}</span><br>
<span>{{itemData.amount}}</span><br>
<span>{{itemData.description}}</span><br>
</div>
</ion-col>
</ion-row>
</ion-item>
这给
import numpy as np
import pandas as pd
import sqlite3
np.random.seed(100)
# database
con = sqlite3.connect("mydb.db")
# dataframes
df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'],
'value': np.random.randn(4)})
df2 = pd.DataFrame({'key': ['B', 'D', 'D', 'E'],
'value': np.random.randn(4)})
print(df1, '\n\n', df2)
# add dataframes to database
df1.to_sql('df1',con,if_exists='replace',index=False)
df1.to_sql('df2',con,if_exists='replace',index=False)
q = """
SELECT *
FROM df1
INNER JOIN df2
ON df1.key = df2.key;
"""
pd.read_sql_query(q,con)
如何获得表格:
key value key value
0 B 0.342680 B 0.981321
1 D -0.252436 D 0.221180
2 D -0.252436 D 0.514219
答案 0 :(得分:2)
使用as
:
SELECT df1.key as key1, df1.value as df1_value, df2.value as df2_value
FROM df1 INNER JOIN
df2
ON df1.key = df2.key;
因为键是相同的,所以不需要包括两个表中的值。
答案 1 :(得分:1)
您需要为列名加上别名。您不想指定SELECT *
构造,而是要指定要返回的列,并使用as
关键字选择结果集中该列的显示名称。
有关更多信息,请参见the sqlite documentation
SELECT
df1.key,
df1.value as df1_value,
df2.value as df2_value
FROM df1
INNER JOIN df2
ON df1.key = df2.key