如何将两列合并为一个(最终输出)(python / sqlite)
import sqlite3
import pandas as pd
# load data
df = pd.read_csv('CurriculumAuditReport.csv')
# strip whitespace from headers
df.columns = df.columns.str.strip()
con = sqlite3.connect("sans.db")
# drop data into database
df.to_sql("MyTable", con, if_exists='replace')
qry = """
SELECT department, count(*) as cnt
FROM MyTable
WHERE CompletedTraining = 'Incomplete'
GROUP BY department
"""
qry2 = """
SELECT [Employee Name], Department, [Date Assigned] FROM MyTable Where CompletedTraining ='Incomplete' ORDER BY Department ASC
"""
df = pd.read_sql_query(qry, con)
df2 = pd.read_sql_query(qry2, con)
print(df.to_json())
print(df2)
con.close()
我可以将部门与cnt合并吗?所以我有AQPSD:6,ASD:8,CO:2等???
目前:预计有2列
Department count(*)
0 AQPSD 6
1 ASD 8
2 CO 2
3 ECARS 3
4 ED 6
5 EO 4
6 ISD 4
7 MSCD 5
8 OIS 1
9 RD 2
10 TTD 4
这有以下输出:1列(难以显示的那种但是我的最终目标)
Department
0 AQPSD 6
1 ASD 8
2 CO 2
3 ECARS 3
4 ED 6
5 EO 4
6 ISD 4
7 MSCD 5
8 OIS 1
9 RD 2
10 TTD 4
答案 0 :(得分:4)
您可以在SQLite端或Pandas中执行此操作。
选项1(使用SQLite):
qry = """
SELECT department || ' ' || cast(count(*) as text) as col_name
FROM MyTable
WHERE CompletedTraining = 'Incomplete'
GROUP BY department
"""
df = pd.read_sql(qry, con)
选项2(使用Pandas):
假设我们有以下DataFrame:
In [79]: df
Out[79]:
department cnt
0 AQPSD 6
1 ASD 8
2 CO 2
3 ECARS 3
4 ED 6
5 EO 4
6 ISD 4
7 MSCD 5
8 OIS 1
9 RD 2
10 TTD 4
让我们将其转换为单列DF:
In [80]: df['department'] = df['department'] + ' ' + df.pop('cnt').astype(str)
In [81]: df
Out[81]:
department
0 AQPSD 6
1 ASD 8
2 CO 2
3 ECARS 3
4 ED 6
5 EO 4
6 ISD 4
7 MSCD 5
8 OIS 1
9 RD 2
10 TTD 4
PS这可以在不使用SQLite的情况下轻松完成,但我们需要一个原始格式的小型可重现样本数据集(可以从CurriculumAuditReport.csv
重现数据)
答案 1 :(得分:0)
这是一步一步的解决方案:
添加新列并将count列转换为带有" astype(str)
的字符串df['new_column'] = df['Department'] + " " + df['count'].astype(str)
删除您不需要的列
del df['Department']
del df['count']
重命名new_column
df.rename(columns={'new_column': 'Department'}, inplace=True)
我知道它有很多步骤,但有时最好通过小步骤将其分解以便更好地理解。