在python中使用dataframe创建SQL查询

2024-09-27 01:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

实际上没有csv文件。我有拼花锉刀。因此,我需要从三个表中提取数据。这些表是publication、section和alt section表 正如您从图像中看到的,我需要以下输出

我有一个这样的数据框,如屏幕截图所示

我需要以数据帧的形式获得以下输出

pub number std kw1   stdkw2
---------------------------    
1078143      T.       Art.

这样,如果同一个数字有3个或3个以上的值,则应将它们全部取为stdkw1、stdkw2、stdkw3等

enter image description hereenter image description here

enter image description here


Tags: 文件csv数据图像number屏幕sectionalt
1条回答
网友
1楼 · 发布于 2024-09-27 01:30:30

按发布号对数据帧进行分组。然后迭代组。将带有发布号的std_section_name值附加到结果列表中。使用结果列表中的数据创建dataframe。稍后将列名添加到数据帧

df = pd.DataFrame([[3333,1078143,'T.'],[3333,1078143,'ssss'],[3334,1078145,'Art'],[3334,1078145,'Art'],[3334,1078145,'Art'],[3334,1078145,'Art'],[3334,1078143,'team']],columns=['section_id','pub_number','std_section_name'])
result = list()
for name,group in  df.groupby(by = ['pub_number']):
    if group.shape[0] < 3:
        continue
    result.append([name] + group['std_section_name'].tolist())
ref = pd.DataFrame(result)
ref.columns = ["pub_number"] + ["stdkw" + str(i) for i in range(1,ref.shape[1])]
print(ref)

相关问题 更多 >

    热门问题