如何解决使用CASE合并成一列,再加上其他列?
例如,我有一张桌子,称为“就业”:
df.groupby('variety').transform(lambda x: x.fillna(x.mode()))
我想做的是建立一个包含以下列的报告:
ID NAME Employer StartDate EndDate
-----------------------------------------------------------------------
20 John Company A 2-5-2015 5-5-2018
20 John Company B 5-10-2018 NULL
100 Bill Company C 10-10-2017 10-15-2018
100 Bill Company D 11-20-2018 3-5-2019
100 Bill Company E 4-5-2019 NULL
例如,基于上表,我想显示:
ID NAME Employer1 Employer2 Employer3
我希望Employer1列始终显示其最近/当前雇主,并从那里降级到Employer2和Employer3列。如果某人只有1个雇主,则Employer2和Employer3应该为NULL,而如果他们只有2个雇主,则Employer3应该为NULL。
解决方法
具有ROW_NUMBER()
窗口功能:
select id,name,max(case when rn = 1 then employer end) Employer1,max(case when rn = 2 then employer end) Employer2,max(case when rn = 3 then employer end) Employer3
from (
select *,row_number() over (partition by id,name order by startdate desc) rn
from employment
) e
group by id,name
order by id,name
请参见demo。
结果:
> id | name | Employer1 | Employer2 | Employer3
> --: | :--- | :-------- | :-------- | :--------
> 20 | John | Company B | Company A | null
> 100 | Bill | Company E | Company D | Company C
,
我认为这样会起作用。条件聚合
File unzip(File zip) throws IOException
{
ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
//helper directory to store files into while program is running!
File helperDir = Files.createDirectories(Paths.get("zipFile")).toFile();
//helperDir.deleteOnExit();
byte[] buffer = new byte[1024];
for (ZipEntry entry; (entry = zis.getNextEntry()) != null; )
{
if (!entry.isDirectory()) //true if file in zip is not a folder
{
File newFile = new File(helperDir,entry.getName());
//newFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(newFile);
for (int len = zis.read(buffer); len > 0; len = zis.read(buffer))
fos.write(buffer,len);
fos.close();
}
else
{
//What to do if there are folders in zip...
}
}
zis.close();
return helperDir;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。