минимальное значение столбца в кадре данных - PullRequest
0 голосов
/ 01 марта 2020

У меня есть следующий код:

from __future__ import division
import pyodbc
import csv
import pandas as pd
import numpy as np

count = 1
dsn = "DRIVER={SQL Server};server=XXXX;database=ABCD"
conn = pyodbc.connect(dsn)
cursor = conn.cursor()
#policy = cursor.execute("select distinct client_num, py_type, py_num, max(ex_date_n) as ex_date_n, max(ef_date_n) as ef_date_n from dbo.policy group by client_num, py_type, py_num")
policy = cursor.execute("select distinct client_num, py_type, py_num, max(ex_date_n) as ex_date_n,max(ef_date_n) as ef_date_n from dbo.policy where client_num = 62961 and py_type = 'A' and py_num = '01' group by client_num, py_type, py_num")
results1 = cursor.fetchall()

for row in results1:
    pol_client_num = row.client_num.strip()
    pol_py_type = row.py_type.strip()
    pol_py_num = row.py_num.strip()
    pol_number = pol_client_num+pol_py_type+pol_py_num
    pol_exp_date = row.ex_date_n
    pol_eff_date = row.ef_date_n
    #related = cursor.execute("select distinct a.client_num,a.py_type,a.py_num,a.rclient_num,a.py_rtype,a.rpy_num from policy_replace a where a.client_num = "+pol_client_num+" and a.py_type = '"+pol_py_type+"' and a.py_num = '"+pol_py_num+"'")
    related = cursor.execute("select distinct a.client_num,a.py_type,a.py_num,a.rclient_num,a.py_rtype,a.rpy_num from policy_replace a where a.client_num = 62961 and a.py_type = 'A' and a.py_num = 01")
    results2 = cursor.fetchall()
    for row in results2:
        rel_client_num = row.rclient_num.strip()
        rel_py_type = row.py_rtype.strip()
        rel_py_num = row.rpy_num.strip()
        rel_pol_number = rel_client_num+rel_py_type+rel_py_num
        related_dates = cursor.execute("select max(b.ex_date_n) as ex_date_n, b.ef_date_n from policy b where b.ex_date_n >= 20200225 and b.client_num = "+rel_client_num+" and b.py_type = '"+rel_py_type+"' and b.py_num = '"+rel_py_num+"' group by b.ef_date_n")
        #related_dates = cursor.execute("select max(b.ex_date_n) as ex_date_n, b.ef_date_n from policy b where b.client_num = 37916 and b.py_type = 'F' and b.py_num = 05 group by b.ef_date_n")
        results3 = cursor.fetchall()
        for row in results3:            
            rel_exp_date = row.ex_date_n
            rel_eff_date = row.ef_date_n
            final_result1 = (pol_number,pol_exp_date,pol_eff_date,rel_pol_number,rel_exp_date,rel_eff_date)
        df = pd.DataFrame(final_result1)
        df = df.transpose()
        df.columns = ['pol_number','pol_exp_date','pol_eff_date','rel_pol_number','rel_exp_date','rel_eff_date']
        df_grouped = df.groupby('pol_number')['rel_exp_date'].min()
        print(df_grouped)
print('done')                

При выполнении следующие данные выводятся: Для результатов1,

'62961 ', 'A', '01', 20210429, 20200429

Для результатов2,

('62961 ', 'A', '01', '62961', 'A', '02'), 
('62961 ', 'A', '01', '62961', 'A', '03'), 
('62961 ', 'A', '01', '63832', 'A', '01')

Для результатов3,

[(20201107, 20191107)]
[(20210407, 20200407)] 
[(20200719, 20190719)] 

Ожидаемый результат будет следующим:

'69621','A','01',20210429,20200429,'69621','A','02',20201107,20191107,'62961','A','03',20210407,20200407,'63832','A','01',20200719,20190719,'63832','A','01',20200719,20190719

Требуемый формат вывода: для каждой строки в результатах1 --- для каждой связанной строки в results2 --- каждая связанная дата в results3 --- минимум exp_date во всех строках и соответствующий pol_num / rel_pol_num. Вот почему я пытаюсь использовать функцию df.min (), чтобы получить min для всех exp_date. Но, похоже, это не делает работу, потому что я, возможно, что-то упускаю. Я также попытался оси = 0, как упоминалось в комментариях, но это не сработало. Любое направление приветствуется.

...