How to plot multiple Seaborn Barplots in a subplot? - seaborn
I want to create a subplot with three seaborn barplots. I have already created the three population pyramid barplots but I do not know how to put them together as subplots.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
'''1980'''
Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000],
'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})
AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']
bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")
bar_plot.set_xticklabels(labels)
'''2020'''
Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})
bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")
bar_plot.set_xticklabels(labels)
'''2050'''
Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})
bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")
bar_plot.set_xticklabels(labels)
Here are the three separate barplots that I have obtained.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
fig, axes = plt.subplots(2, 2)
Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000],
'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})
Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})
Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})
AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']
bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")
bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")
bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")
Here are the changes I made, I have tried the ax=axes[0] just for the first barplot.
It's easier when you flatten the axes:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
fig, axes = plt.subplots(2, 2)
axes = axes.flatten()
Population1980 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-49228000, -61283000, -64391000, -52437000, -42955000, -44667000, -31570000, -23887000, -22390000, -20971000, -17685000, -15450000, -13932000, -11020000, -7611000, -4653000, -1952000, -625000, -116000, -14000, -1000],
'Female': [52367000, 64959000, 67161000, 55388000, 45448000, 47129000, 33436000, 26710000, 25627000, 23612000, 20075000, 16368000, 14220000, 10125000, 5984000, 3131000, 1151000, 312000, 49000, 4000, 0]})
Population2020 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-39476000, -40415000, -38913000, -38239000, -40884000, -46466000, -62296000, -48746000, -46985000, -58664000, -61097000, -48782000, -38597000, -37623000, -23525000, -14337000, -9298000, -4739000, -1574000, -359000, -62000],
'Female': [44456000, 46320000, 45350000, 44103000, 46274000, 51523000, 66443000, 51346000, 49289000, 61173000, 62348000, 49958000, 38917000, 36527000, 21425000, 12207000, 6884000, 2843000, 731000, 116000, 13000]})
Population2050 = pd.DataFrame({'Age': ['0-4','5-9','10-14','15-19','20-24','25-29','30-34','35-39','40-44','45-49','50-54','55-59','60-64','65-69','70-74','75-79','80-84','85-89','90-94','95-99','100+'],
'Male': [-31222000, -32130000, -32532000, -33006000, -33639000, -35628000, -38650000, -39462000, -37812000, -37015000, -39486000, -44586000, -58817000, -44365000, -39900000, -43830000, -36255000, -19327000, -7942000, -2883000, -497000],
'Female': [33392000, 34351000, 34764000, 35250000, 36576000, 39416000, 43473000, 45150000, 43954000, 42485000, 44282000, 48656000, 61036000, 44548000, 38445000, 39264000, 28884000, 13627000, 4539000, 1207000, 123000]})
AgeClass = ['100+','95-99','90-94','85-89','80-84','75-79','70-74','65-69','60-64','55-59','50-54','45-49','40-44','35-39','30-34','25-29','20-24','15-19','10-14','5-9','0-4']
labels = ['80M', '60M', '40M', '20M', '0', '20M', '40M', '60M']
bar_plot = sns.barplot(x='Male', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='OrRd', lw=0)
bar_plot = sns.barplot(x='Female', y='Age', data=Population1980, order=AgeClass, orient='h', ax=axes[0], palette='PuBu', lw=0)
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "1980")
bar_plot = sns.barplot(x='Male', y='Age', data=Population2020, order=AgeClass, palette='OrRd', lw=0, ax=axes[1])
bar_plot = sns.barplot(x='Female', y='Age', data=Population2020, order=AgeClass, palette='PuBu', lw=0, ax=axes[1])
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2020")
bar_plot = sns.barplot(x='Male', y='Age', data=Population2050, order=AgeClass, palette='OrRd', lw=0, ax=axes[2])
bar_plot = sns.barplot(x='Female', y='Age', data=Population2050, order=AgeClass, palette='PuBu', lw=0, ax=axes[2])
bar_plot.set_xticklabels(labels)
#bar_plot.set(xlabel="Population by sex", ylabel="Age-Group", title = "2050")
The reason to flatten is that axes is an array of 2 x 2, so you have to use 2 indexes to get the ax you want. This is easier with axes.flatten(), because converts the array from 2 x 2 to 1 x 4 dimension, so, you only need one index.
Related
power query split long query into multiple queries - Any Benefits?
I have a query which is (round numbers) 40 steps in length. Most are Add/Transform Columns but there are about 8 merge queried embedded There are multiple data feeds upfront (handled by other queries) which feed into this query (as the root source of data as well as for the merge queries). The number of records we are talking from each is no more than 10,000. The above main query takes forever to execute. Am asking whether there is any benefit to splitting the above 40 step query into several (using the Extract Previous functionality). Or in lieu of splitting the query, any ideas on how to speed things up would be most helpful. Added chunks of code (non-sequential since wanted to show various line executions let Source = RMD_Input_File, #"Added Index" = Table.AddIndexColumn(Source, "Index", 2, 1, Int64.Type), #"SSN - Text" = Table.AddColumn(#"Added Index", "SSN - Text", each Number.ToText([SSN],"000-00-0000"),type text), #"Reordered Columns" = Table.ReorderColumns(#"SSN - Text",{"Index", "NameLast", "NameFirst", "NameMi", "SSN", "SSN - Text","PartDOB", "PartDOD", "AnnuityFlag", "Status1", "Status2", "YearsOfSvc", "SpouseSSN", "SpouseDOB", "SpouseDOD", "AcctType", "PartName", "Address1", "Address2", "City", "State", "ZIP", "DCBC Conversion Date", "Plan-1", "Ext-1", "Status-1", "HoldFlag-1", "HoldReason-1", "CurBal-1", "EOYBal-1", "TermDt-1", "Plan-2", "Ext-2", "Status-2", "HoldFlag-2", "HoldReason-2", "CurBal-2", "EOYBal-2", "TermDt-2", "Plan-3", "Ext-3", "Status-3", "HoldFlag-3", "HoldReason-3", "CurBal-3", "EOYBal-3", "TermDt-3", "Plan-4", "Ext-4", "Status-4", "HoldFlag-4", "HoldReason-4", "CurBal-4", "EOYBal-4", "TermDt-4", "Plan-5", "Ext-5", "Status-5", "HoldFlag-5", "HoldReason-5", "CurBal-5", "EOYBal-5", "TermDt-5", "Plan-6", "Ext-6", "Status-6", "HoldFlag-6", "HoldReason-6", "CurBal-6", "EOYBal-6", "TermDt-6", "Plan-7", "Ext-7", "Status-7", "HoldFlag-7", "HoldReason-7", "CurBal-7", "EOYBal-7", "TermDt-7", "Plan-8", "Ext-8", "Status-8", "HoldFlag-8", "HoldReason-8", "CurBal-8", "EOYBal-8", "TermDt-8", "Plan-9", "Ext-9", "Status-9", "HoldFlag-9", "HoldReason-9", "CurBal-9", "EOYBal-9", "TermDt-9", "Plan-10", "Ext-10", "Status-10", "HoldFlag-10", "HoldReason-10", "CurBal-10", "EOYBal-10", "TermDt-10", "Plan-11", "Ext-11", "Status-11", "HoldFlag-11", "HoldReason-11", "CurBal-11", "EOYBal-11", "TermDt-11", "RET_TERM_DATE", "NOTE"}), #"Replaced Value" = Table.ReplaceValue(#"Reordered Columns",null,"",Replacer.ReplaceValue,{"Status-1", "Status-2", "Status-3", "Status-4", "Status-5", "Status-6", "Status-7", "Status-8", "Status-9", "Status-10", "Status-11"}), #"1 in Clergy Plan" = Table.AddColumn(#"Replaced Value", "1 in Clergy Plan", each ClergyPlan([#"Plan-1"])), UpdateStatus1 = Table.ReplaceValue(#"1 in Clergy Plan", each [#"Status-1"], each if [#"Status-1"]>= 20 then [#"Status-1"] else if [1 in Clergy Plan]="Yes" and [PartDOB] < Vlookup("Start Initial", RMD_Dates, "Category", "Date") then 32 else [#"Status-1"], Replacer.ReplaceValue,{"Status-1"}), #"Active in Plan 1" = Table.AddColumn(UpdateStatus1, "Active in Plan 1", each ActiveInPlan([#"TermDt-1"], [#"Status-1"])), #"2 in Clergy Plan" = Table.AddColumn(#"Active in Plan 1", "2 in Clergy Plan", each ClergyPlan([#"Plan-2"])), UpdateStatus2 = Table.ReplaceValue(#"2 in Clergy Plan", each [#"Status-2"], each if [#"Status-2"]>= 20 then [#"Status-2"] else if [2 in Clergy Plan]="Yes" and [PartDOB] < Vlookup("Start Initial", RMD_Dates, "Category", "Date") then 32 else [#"Status-2"], Replacer.ReplaceValue,{"Status-2"}), #"Active in Plan 2" = Table.AddColumn(UpdateStatus2, "Active in Plan 2", each ActiveInPlan([#"TermDt-2"], [#"Status-2"])), #"Max Term Date" = Table.AddColumn(#"Active in Plan 11", "Max Term Date", each List.Max({[#"TermDt-1"],[#"TermDt-2"],[#"TermDt-3"],[#"TermDt-4"],[#"TermDt-5"],[#"TermDt-6"],[#"TermDt-7"],[#"TermDt-8"],[#"TermDt-9"],[#"TermDt-10"],[#"TermDt-11"]}),type date), #"Min Term Date" = Table.AddColumn(#"Max Term Date", "Min Term Date", each List.Min({[#"TermDt-1"],[#"TermDt-2"],[#"TermDt-3"],[#"TermDt-4"],[#"TermDt-5"],[#"TermDt-6"],[#"TermDt-7"],[#"TermDt-8"],[#"TermDt-9"],[#"TermDt-10"],[#"TermDt-11"]}),type date), RBY = Table.AddColumn(#"Year Reach RMD Age", "RBY", each if [RET_TERM_DATE]=null and [Max Term Date]=null then "" else List.Max({Date.Year([RET_TERM_DATE]),[Year Reach RMD Age],Date.Year([Max Term Date])})), Initial_or_Reocurring = Table.AddColumn(RBY, "Initial_or_Reocurring", each if [RBY]="" or [RBY]>RMD_Year then "No RMD Due" else if [RBY]=RMD_Year then "Initial" else "Recurring"), #"0 in Any Plan" = Table.AddColumn(Initial_or_Reocurring, "0 in Any Plan", each if List.Contains({[#"Status-1"],[#"Status-2"],[#"Status-3"],[#"Status-4"],[#"Status-5"],[#"Status-6"],[#"Status-7"],[#"Status-8"],[#"Status-9"],[#"Status-10"],[#"Status-11"]},0) then "Yes" else "No"), #"Missing/Foreign Address" = Table.AddColumn(#"0 in Any Plan", "Missing/Foreign Address", each if [ZIP]= null then "Foreign" else if List.AnyTrue({[Address1]=null,[City]=null, [State]=null}) then "Missing" else null), ClergyLay = Table.AddColumn(#"Missing/Foreign Address", "ClergyLay", each if List.NonNullCount(List.Distinct({[1 in Clergy Plan],[2 in Clergy Plan],[3 in Clergy Plan],[4 in Clergy Plan],[5 in Clergy Plan],[6 in Clergy Plan],[7 in Clergy Plan],[8 in Clergy Plan],[9 in Clergy Plan],[10 in Clergy Plan],[11 in Clergy Plan]}))=1 then (if List.First(List.Distinct(List.RemoveNulls({[1 in Clergy Plan],[2 in Clergy Plan],[3 in Clergy Plan],[4 in Clergy Plan],[5 in Clergy Plan],[6 in Clergy Plan],[7 in Clergy Plan],[8 in Clergy Plan],[9 in Clergy Plan],[10 in Clergy Plan],[11 in Clergy Plan]}))) ="Yes" then "Clergy" else "Lay") else "ClergyLay"), Status = Table.AddColumn(ClergyLay, "Status", each if Vlookup([Status2], status2, "Status2", "Result") = "Deceased" then "Deceased" else if Vlookup([Status2], status2, "Status2", "Result") = "Active" and [Status1]<>"LE" and [PartDOB]<Vlookup("Start Initial", RMD_Dates, "Category", "Date") and [ClergyLay]= "Clergy" then "Inactive - 72" else Vlookup([Status2], status2, "Status2", "Result")), #"active in clergy plan or active clergy" = Table.AddColumn(Status, "active in clergy plan or active clergy", each if List.AnyTrue({ [1 in Clergy Plan]="Yes" and [#"Status-1"]<19, [2 in Clergy Plan]="Yes" and [#"Status-2"]<19, [3 in Clergy Plan]="Yes" and [#"Status-3"]<19, [4 in Clergy Plan]="Yes" and [#"Status-4"]<19, [5 in Clergy Plan]="Yes" and [#"Status-5"]<19, [6 in Clergy Plan]="Yes" and [#"Status-6"]<19, [7 in Clergy Plan]="Yes" and [#"Status-7"]<19, [8 in Clergy Plan]="Yes" and [#"Status-8"]<19, [9 in Clergy Plan]="Yes" and [#"Status-9"]<19, [10 in Clergy Plan]="Yes" and [#"Status-10"]<19, [11 in Clergy Plan]="Yes" and [#"Status-11"]<19, [Status1]<>"LE" and [Status1]<>"" and [Status]="Active" }) then "Yes" else "No"), #"RMD YN-U" = Table.AddColumn(#"active in clergy plan or active clergy", "RMD YN-U", each if List.AnyTrue( {Text.Start([#"Plan-1"],1)="U" and [#"Status-1"]>=20, Text.Start([#"Plan-2"],1)="U" and [#"Status-2"]>=20, Text.Start([#"Plan-3"],1)="U" and [#"Status-3"]>=20, Text.Start([#"Plan-4"],1)="U" and [#"Status-4"]>=20, Text.Start([#"Plan-5"],1)="U" and [#"Status-5"]>=20, Text.Start([#"Plan-6"],1)="U" and [#"Status-6"]>=20, Text.Start([#"Plan-7"],1)="U" and [#"Status-7"]>=20, Text.Start([#"Plan-8"],1)="U" and [#"Status-8"]>=20, Text.Start([#"Plan-9"],1)="U" and [#"Status-9"]>=20, Text.Start([#"Plan-10"],1)="U" and [#"Status-10"]>=20, Text.Start([#"Plan-11"],1)="U" and [#"Status-11"]>=20 } ) then "RMD" else "No RMD"), #"Status-U" = Table.AddColumn(#"RMD YN-U", "Status-U", each Text.Combine({ if Text.Start([#"Plan-1"],1)="U" then Number.ToText([#"Status-1"]) else "", if Text.Start([#"Plan-2"],1)="U" then Number.ToText([#"Status-2"]) else "", if Text.Start([#"Plan-3"],1)="U" then Number.ToText([#"Status-3"]) else "", if Text.Start([#"Plan-4"],1)="U" then Number.ToText([#"Status-4"]) else "", if Text.Start([#"Plan-5"],1)="U" then Number.ToText([#"Status-5"]) else "", if Text.Start([#"Plan-6"],1)="U" then Number.ToText([#"Status-6"]) else "", if Text.Start([#"Plan-7"],1)="U" then Number.ToText([#"Status-7"]) else "", if Text.Start([#"Plan-8"],1)="U" then Number.ToText([#"Status-8"]) else "", if Text.Start([#"Plan-9"],1)="U" then Number.ToText([#"Status-9"]) else "", if Text.Start([#"Plan-10"],1)="U" then Number.ToText([#"Status-10"]) else "", if Text.Start([#"Plan-11"],1)="U" then Number.ToText([#"Status-11"]) else "" },",")), #"adjCurBal-U" = Table.AddColumn(#"EOYBal-U", "adjCurBal-U", each if [active in clergy plan or active clergy] = "Yes" then List.Sum( {if List.AnyTrue({Text.Start([#"Plan-1"],2)="UM",Text.Start([#"Plan-1"],2)="UC",[#"Plan-1"]="U99999"}) then 0 else if Text.Start([#"Plan-1"],1)="U" and [#"Status-1"]>19 then [#"CurBal-1"] else 0, if List.AnyTrue({Text.Start([#"Plan-2"],2)="UM",Text.Start([#"Plan-2"],2)="UC",[#"Plan-2"]="U99999"}) then 0 else if Text.Start([#"Plan-2"],1)="U" and [#"Status-2"]>19 then [#"CurBal-2"] else 0, if List.AnyTrue({Text.Start([#"Plan-3"],2)="UM",Text.Start([#"Plan-3"],2)="UC",[#"Plan-3"]="U99999"}) then 0 else if Text.Start([#"Plan-3"],1)="U" and [#"Status-3"]>19 then [#"CurBal-3"] else 0, if List.AnyTrue({Text.Start([#"Plan-4"],2)="UM",Text.Start([#"Plan-4"],2)="UC",[#"Plan-4"]="U99999"}) then 0 else if Text.Start([#"Plan-4"],1)="U" and [#"Status-4"]>19 then [#"CurBal-4"] else 0, if List.AnyTrue({Text.Start([#"Plan-5"],2)="UM",Text.Start([#"Plan-5"],2)="UC",[#"Plan-5"]="U99999"}) then 0 else if Text.Start([#"Plan-5"],1)="U" and [#"Status-5"]>19 then [#"CurBal-5"] else 0, if List.AnyTrue({Text.Start([#"Plan-6"],2)="UM",Text.Start([#"Plan-6"],2)="UC",[#"Plan-6"]="U99999"}) then 0 else if Text.Start([#"Plan-6"],1)="U" and [#"Status-6"]>19 then [#"CurBal-6"] else 0, if List.AnyTrue({Text.Start([#"Plan-7"],2)="UM",Text.Start([#"Plan-7"],2)="UC",[#"Plan-7"]="U99999"}) then 0 else if Text.Start([#"Plan-7"],1)="U" and [#"Status-7"]>19 then [#"CurBal-7"] else 0, if List.AnyTrue({Text.Start([#"Plan-8"],2)="UM",Text.Start([#"Plan-8"],2)="UC",[#"Plan-8"]="U99999"}) then 0 else if Text.Start([#"Plan-8"],1)="U" and [#"Status-8"]>19 then [#"CurBal-8"] else 0, if List.AnyTrue({Text.Start([#"Plan-9"],2)="UM",Text.Start([#"Plan-9"],2)="UC",[#"Plan-9"]="U99999"}) then 0 else if Text.Start([#"Plan-9"],1)="U" and [#"Status-9"]>19 then [#"CurBal-9"] else 0, if List.AnyTrue({Text.Start([#"Plan-10"],2)="UM",Text.Start([#"Plan-10"],2)="UC",[#"Plan-10"]="U99999"}) then 0 else if Text.Start([#"Plan-10"],1)="U" and [#"Status-10"]>19 then [#"CurBal-10"] else 0, if List.AnyTrue({Text.Start([#"Plan-11"],2)="UM",Text.Start([#"Plan-11"],2)="UC",[#"Plan-11"]="U99999"}) then 0 else if Text.Start([#"Plan-11"],1)="U" and [#"Status-11"]>19 then [#"CurBal-11"] else 0} ) else List.Sum({if Text.Start([#"Plan-1"],1)="U" and [#"Status-1"]>19 then [#"CurBal-1"] else 0, if Text.Start([#"Plan-2"],1)="U" and [#"Status-2"]>19 then [#"CurBal-2"] else 0, if Text.Start([#"Plan-3"],1)="U" and [#"Status-3"]>19 then [#"CurBal-3"] else 0, if Text.Start([#"Plan-4"],1)="U" and [#"Status-4"]>19 then [#"CurBal-4"] else 0, if Text.Start([#"Plan-5"],1)="U" and [#"Status-5"]>19 then [#"CurBal-5"] else 0, if Text.Start([#"Plan-6"],1)="U" and [#"Status-6"]>19 then [#"CurBal-6"] else 0, if Text.Start([#"Plan-7"],1)="U" and [#"Status-7"]>19 then [#"CurBal-7"] else 0, if Text.Start([#"Plan-8"],1)="U" and [#"Status-8"]>19 then [#"CurBal-8"] else 0, if Text.Start([#"Plan-9"],1)="U" and [#"Status-9"]>19 then [#"CurBal-9"] else 0, if Text.Start([#"Plan-10"],1)="U" and [#"Status-10"]>19 then [#"CurBal-10"] else 0, if Text.Start([#"Plan-11"],1)="U" and [#"Status-11"]>19 then [#"CurBal-11"] else 0 })), #"adjEOYBal-U" = Table.AddColumn(#"adjCurBal-U", "adjEOYBal-U", each if [active in clergy plan or active clergy] = "Yes" then List.Sum( {if List.AnyTrue({Text.Start([#"Plan-1"],2)="UM",Text.Start([#"Plan-1"],2)="UC",[#"Plan-1"]="U99999"}) then 0 else if Text.Start([#"Plan-1"],1)="U" and [#"Status-1"]>19 then [#"EOYBal-1"] else 0, if List.AnyTrue({Text.Start([#"Plan-2"],2)="UM",Text.Start([#"Plan-2"],2)="UC",[#"Plan-2"]="U99999"}) then 0 else if Text.Start([#"Plan-2"],1)="U" and [#"Status-2"]>19 then [#"EOYBal-2"] else 0, if List.AnyTrue({Text.Start([#"Plan-3"],2)="UM",Text.Start([#"Plan-3"],2)="UC",[#"Plan-3"]="U99999"}) then 0 else if Text.Start([#"Plan-3"],1)="U" and [#"Status-3"]>19 then [#"EOYBal-3"] else 0, if List.AnyTrue({Text.Start([#"Plan-4"],2)="UM",Text.Start([#"Plan-4"],2)="UC",[#"Plan-4"]="U99999"}) then 0 else if Text.Start([#"Plan-4"],1)="U" and [#"Status-4"]>19 then [#"EOYBal-4"] else 0, if List.AnyTrue({Text.Start([#"Plan-5"],2)="UM",Text.Start([#"Plan-5"],2)="UC",[#"Plan-5"]="U99999"}) then 0 else if Text.Start([#"Plan-5"],1)="U" and [#"Status-5"]>19 then [#"EOYBal-5"] else 0, if List.AnyTrue({Text.Start([#"Plan-6"],2)="UM",Text.Start([#"Plan-6"],2)="UC",[#"Plan-6"]="U99999"}) then 0 else if Text.Start([#"Plan-6"],1)="U" and [#"Status-6"]>19 then [#"EOYBal-6"] else 0, if List.AnyTrue({Text.Start([#"Plan-7"],2)="UM",Text.Start([#"Plan-7"],2)="UC",[#"Plan-7"]="U99999"}) then 0 else if Text.Start([#"Plan-7"],1)="U" and [#"Status-7"]>19 then [#"EOYBal-7"] else 0, if List.AnyTrue({Text.Start([#"Plan-8"],2)="UM",Text.Start([#"Plan-8"],2)="UC",[#"Plan-8"]="U99999"}) then 0 else if Text.Start([#"Plan-8"],1)="U" and [#"Status-8"]>19 then [#"EOYBal-8"] else 0, if List.AnyTrue({Text.Start([#"Plan-9"],2)="UM",Text.Start([#"Plan-9"],2)="UC",[#"Plan-9"]="U99999"}) then 0 else if Text.Start([#"Plan-9"],1)="U" and [#"Status-9"]>19 then [#"EOYBal-9"] else 0, if List.AnyTrue({Text.Start([#"Plan-10"],2)="UM",Text.Start([#"Plan-10"],2)="UC",[#"Plan-10"]="U99999"}) then 0 else if Text.Start([#"Plan-10"],1)="U" and [#"Status-10"]>19 then [#"EOYBal-10"] else 0, if List.AnyTrue({Text.Start([#"Plan-11"],2)="UM",Text.Start([#"Plan-11"],2)="UC",[#"Plan-11"]="U99999"}) then 0 else if Text.Start([#"Plan-11"],1)="U" and [#"Status-11"]>19 then [#"EOYBal-11"] else 0} ) else List.Sum({if Text.Start([#"Plan-1"],1)="U" and [#"Status-1"]>19 then [#"EOYBal-1"] else 0, if Text.Start([#"Plan-2"],1)="U" and [#"Status-2"]>19 then [#"EOYBal-2"] else 0, if Text.Start([#"Plan-3"],1)="U" and [#"Status-3"]>19 then [#"EOYBal-3"] else 0, if Text.Start([#"Plan-4"],1)="U" and [#"Status-4"]>19 then [#"EOYBal-4"] else 0, if Text.Start([#"Plan-5"],1)="U" and [#"Status-5"]>19 then [#"EOYBal-5"] else 0, if Text.Start([#"Plan-6"],1)="U" and [#"Status-6"]>19 then [#"EOYBal-6"] else 0, if Text.Start([#"Plan-7"],1)="U" and [#"Status-7"]>19 then [#"EOYBal-7"] else 0, if Text.Start([#"Plan-8"],1)="U" and [#"Status-8"]>19 then [#"EOYBal-8"] else 0, if Text.Start([#"Plan-9"],1)="U" and [#"Status-9"]>19 then [#"EOYBal-9"] else 0, if Text.Start([#"Plan-10"],1)="U" and [#"Status-10"]>19 then [#"EOYBal-10"] else 0, if Text.Start([#"Plan-11"],1)="U" and [#"Status-11"]>19 then [#"EOYBal-11"] else 0 })),
Convert xls files to csv files in apache NIFI
Hello guys I'm trying to convert .xls files to .csv in apache NIFI data flow. I tried many solutions with no result I even tried creating a script like this sccript Thank you in advance
Configure your ExecuteStreamCommand as and try using below code in your python script, import csv import os import sys from io import StringIO, BytesIO import pandas as pd import xlrd from pandas import ExcelFile wb = xlrd.open_workbook(file_contents=sys.stdin.read(),logfile=open(os.devnull, 'w')) excel_file_df = pd.read_excel(wb, sheet_name='Sheet1', index=False, index_col=0, encoding='utf-8',engine='xlrd') #flowfile_content = ExcelFile(BytesIO(sys.stdin.read())) #excel_file_df = pd.read_excel(flowfile_content, sheet_name='Sheet1', index=False, index_col=0, encoding='utf-8') csv_data_rows = [] header_list = list(excel_file_df.columns.values) temp_header_list = [] for field in header_list: temp = '"' + field + '"' temp_header_list.append(temp) header_row = ','.join([str(elem) for elem in temp_header_list]) csv_data_rows.append(header_row) is_header_row = True for index, row in excel_file_df.iterrows(): if is_header_row : is_header_row = False continue temp_data_list = [] for item in row : #item = item.encode('utf-8', 'ignore').decode('utf-8') if hasattr(item, 'encode'): item = item.encode('ascii', 'ignore').decode('ascii') item = str(item) item = item.replace('\n', '') item = item.replace('",', '" ') if item == 'nan': item='' temp = '"' + str(item) + '"' temp_data_list.append(temp) data_row = ','.join([str(elem) for elem in temp_data_list]) data_row = data_row csv_data_rows.append(data_row) for item in csv_data_rows: sys.stdout.write("%s\r\n" % item)
Did you try the ConvertExcelToCSVProcessor? If so and it didn't work can you share any errors, logs, etc.?
import csv import os import io import sys from io import StringIO, BytesIO import pandas as pd import xlrd from pandas import ExcelFile import petl as etl #xls = etl.fromxls(sys.stdin) #wb = xlrd.open_workbook(file_contents=xls,logfile=open(os.devnull, 'w', encoding='utf-8')) excel_file_df = pd.read_excel(sys.stdin.buffer, sheet_name='Sheet1', index=False, index_col=0, encoding='utf-8',engine='xlrd') #flowfile_content = ExcelFile(BytesIO(sys.stdin.read())) #excel_file_df = pd.read_excel(flowfile_content, sheet_name='Sheet1', index=False, index_col=0, encoding='utf-8') csv_data_rows = [] header_list = list(excel_file_df.columns.values) temp_header_list = [] for field in header_list: temp = '"' + field + '"' temp_header_list.append(temp) header_row = ','.join([str(elem) for elem in temp_header_list]) csv_data_rows.append(header_row) is_header_row = True for index, row in excel_file_df.iterrows(): if is_header_row : is_header_row = False continue temp_data_list = [] for item in row : #item = item.encode('utf-8', 'ignore').decode('utf-8') if hasattr(item, 'encode'): item = item.encode('ascii', 'ignore').decode('ascii') item = str(item) item = item.replace('\n', '') item = item.replace('",', '" ') if item == 'nan': item='' temp = '"' + str(item) + '"' temp_data_list.append(temp) data_row = ','.join([str(elem) for elem in temp_data_list]) data_row = data_row csv_data_rows.append(data_row) for item in csv_data_rows: sys.stdout.write("%s\r\n" % item)
PyQt5 using 2 .show() can't get ride of either one
PyQt5 App, I am building this app and had to use 2 .show(), 2 GUI show up when I run the code. One blank GUI and with with my information. When I remove the first .show() only the blank GUI shows up when I remove the second one nothing shows up any ideas. import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QGridLayout, `enter code here`QLineEdit, QLabel from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFormLayout, QPushButton, `enter code here`QTableWidget, QTableWidgetItem from PyQt5.QtCore import Qt import sqlite3 class ExerciseTracker(QWidget): def __init__(self): super().__init__() self.myWindow = QWidget() self.title = 'Exercise Tracker' self.setWindowTitle(self.title) self.setGeometry(200, 400, 300, 200) self.move(60, 15) self.layout = QFormLayout() self.layout.addRow(QLabel('<h2>Welcome to the App!</h2>', parent=self.myWindow)) line_edit1 = QLineEdit() self.layout.addRow('Day of the week: ', line_edit1) line_edit2 = QLineEdit() self.layout.addRow('Body Part: ', line_edit2) line_edit3 = QLineEdit() self.layout.addRow('Input Exercise: ', line_edit3) line_edit4 = QLineEdit() self.layout.addRow('Input Sets: ', line_edit4) line_edit5 = QLineEdit() self.layout.addRow('Input Reps: ', line_edit5) btn1 = QPushButton('Submit') self.layout.addRow(btn1) btn2 = QPushButton('Show Records') self.layout.addRow(btn2) self.myWindow.setLayout(self.layout) self.myWindow.show() def main(): exercise = QApplication(sys.argv) view = ExerciseTracker() view.show() sys.exit(exercise.exec_()) if __name__ =='__main__': main()
Is there any way to retrieve real time data from sql server on GUI interface with wxpython?
I have a simple application about GUI with wxpython , I'd like to display real time value from database. Currently what I can do is to display the value from database , but I can not figure out how to make it REAL TIME , I mean when the data changed in database then I hope the data on this interface will synchronize with database. Hope it's clear. Anyone who can help on this ? Any suggestion will be appreciated. Here I attached my code: Python 3.5 , Win 10 # -*- coding: utf-8 -*- import wx import wx.adv import wx.grid import sys import pyodbc import time bgcolor = (220,220,220) class Mywin(wx.Frame): def __init__(self, parent, title): super(Mywin, self).__init__(parent, title = title, size = (600,320)) self.InitUI() def InitUI(self): nb = wx.Notebook(self) nb.AddPage(MyPanel3(nb), "Table") self.Centre() self.Show(True) def retrieve_data_fromdb(): # SQL Server configuration server = 'localhost' db = '***' user = 'sa' pwd = '******' src_db = pyodbc.connect(r'DRIVER={SQL Server Native Client 11.0};SERVER=' + server + ';DATABASE=' + db + ';UID=' + user + ';PWD=' + pwd) cur = src_db.cursor() select = 'select * from real_time_test' cur.execute(select) rows = cur.fetchone() wind_spd = rows[0] site_pwr = rows[1] acv_pr_setpnt = rows[2] park_pbl_cap = rows[3] tol_cur = rows[4] tol_non_prod = rows[5] data = [] data.append(wind_spd) data.append(site_pwr) data.append(acv_pr_setpnt) data.append(park_pbl_cap) data.append(tol_cur) data.append(tol_non_prod) return data cur.commit() cur.close() src_db.close() class MyPanel3(wx.Panel): def __init__(self, parent): super(MyPanel3, self).__init__(parent) self.SetBackgroundColour(bgcolor) self.Bind(wx.EVT_PAINT, self.OnPaint) title_NDC = wx.StaticText(self, -1, " Real time signals ", (30, 22)) title_NDC.SetForegroundColour((0, 0, 255)) wx.StaticText(self, -1, "1. Wind Speed", (35, 75)) wx.StaticText(self, -1, "2. Site Power", (35, 95)) wx.StaticText(self, -1, "Instant", (300, 45)) wx.StaticText(self, -1, "m/s", (340, 75)) wx.StaticText(self, -1, "kW", (340, 95)) a = retrieve_data_fromdb() wind_spd_val = wx.StaticText(self, -1, a[0], (300, 75)) wind_spd_val.SetForegroundColour((0, 0, 255)) def OnPaint(self, event): pdc = wx.PaintDC(self) gc = wx.GCDC(pdc) gc.Clear() brush_rec = wx.Brush(bgcolor) gc.SetBrush(brush_rec) gc.SetPen(wx.Pen("black", 2)) x1 = 20 y1 = 30 w1 = 500 h1 = 180 radius = 3 gc.DrawRoundedRectangle(x1, y1, w1, h1, radius) ex = wx.App() Mywin(None,'My example') ex.MainLoop()
An easy, if inefficient, way to achieve this is using a simple Timer. Simply read the database once every x amount of time and update the screen. Forgive the messy code but you'll get the idea. Use the input box to update the value. You'll have to fiddle with the database code, as I've used sqlite3, which is already on my box. import wx import sqlite3 class Frame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None) #Dummy database self.data = [['1111','Lots'],['2222','Fast'],['3333','Low']] self.db = sqlite3.connect("test1.db") self.cursor = self.db.cursor() self.cursor.execute('CREATE TABLE IF NOT EXISTS TEST(Id Text, Value Text)') self.cursor.execute("select * from TEST"); test = self.cursor.fetchone() if not test: for i in self.data: result = self.db.execute("insert into Test (Id, Value) values (?,?)",(i[0],i[1])) self.db.commit() wx.StaticText(self, -1, "1. Wind Speed", (35, 75)) wx.StaticText(self, -1, "2. Site Power", (35, 95)) wx.StaticText(self, -1, "Instant", (300, 45)) wx.StaticText(self, -1, "m/s", (340, 75)) wx.StaticText(self, -1, "kW", (340, 95)) self.wind = wx.TextCtrl(self, -1, pos=(35,200),style=wx.TE_RIGHT|wx.TE_PROCESS_ENTER) a = self.retrieve_data_fromdb() self.wind_spd_val = wx.StaticText(self, -1, a[1], (300, 75)) #Bind the callbacks for the timer and the update function self.wind.Bind(wx.EVT_TEXT_ENTER, self.OnUpdate) self.dbtimer = wx.Timer(self) self.Bind(wx.EVT_TIMER,self.OnTimer, self.dbtimer) self.dbtimer.Start(1000) def retrieve_data_fromdb(self): self.cursor.execute("SELECT * FROM Test where Id = ?",['2222']) a = self.cursor.fetchone() return a def OnUpdate(self,event): self.cursor.execute("update Test set Value=? where Id=?" , (self.wind.GetValue(),'2222')) self.db.commit() def OnTimer(self,event): a = self.retrieve_data_fromdb() self.wind_spd_val.SetLabel(a[1]) print("Updated with "+str(a[1])) if __name__ == '__main__': app = wx.App() frame = Frame() frame.Show() app.MainLoop()
Tensorflow: How to take advantage of multi GPUs?
I have a CNN which run well with 1 GPU. Now I move to another computer which has 2 GPUs, I would like to train my network using both GPUs to save time. How could I do it? I read the https://www.tensorflow.org/tutorials/using_gpu but I think the example was too simple and honestly I don't know how to apply it on my real network. Could anyone give me a simple illustration on my network please? (I'm doing AutoEncoder). Thank you very much! graphCNN = tf.Graph() with graphCNN.as_default(): # Input x = tf.placeholder(tf.float32, shape=(None, img_w, img_h,img_ch), name="X") # X # Output expected y_ = tf.placeholder(tf.float32, shape=(None, img_w, img_h,img_ch), name="Y") # Y_ # Dropout dropout = tf.placeholder(tf.float32) ### Model def model(data): ### Encoder c64 = ConvLayer(data, depth_in=1, depth_out=64, name="c64", kernel_size=3, acti=True) c128 = ConvLayer(c64, depth_in=64, depth_out=128, name="c128", kernel_size=3, acti=True) c256 = ConvLayer(c128, depth_in=128, depth_out=256, name="c256", kernel_size=3, acti=True) c512_1 = ConvLayer(c256, depth_in=256, depth_out=512, name="c512_1", kernel_size=3, acti=True) c512_2 = ConvLayer(c512_1, depth_in=512, depth_out=512, name="c512_2", kernel_size=3, acti=True) c512_3 = ConvLayer(c512_2, depth_in=512, depth_out=512, name="c512_3", kernel_size=3, acti=True) c512_4 = ConvLayer(c512_3, depth_in=512, depth_out=512, name="c512_4", kernel_size=3, acti=True) c512_5 = ConvLayer(c512_4, depth_in=512, depth_out=512, name="c512_5", kernel_size=3, acti=True) ### Decoder dc512_5 = DeconvLayer(c512_5, depth_in=512, depth_out=512, name="dc512_5", kernel_size=3, acti=True) dc512_4 = DeconvLayer(dc512_5, depth_in=512, depth_out=512, name="dc512_4", kernel_size=3, acti=True) dc512_3 = DeconvLayer(dc512_4, depth_in=512, depth_out=512, name="dc512_3", kernel_size=3, acti=True) dc512_2 = DeconvLayer(dc512_3, depth_in=512, depth_out=512, name="dc512_2", kernel_size=3, acti=True) dc512_1 = DeconvLayer(dc512_2, depth_in=512, depth_out=512, name="dc512_1", kernel_size=3, acti=True) dc256 = DeconvLayer(dc512_1, depth_in=512, depth_out=256, name="dc256", kernel_size=3, acti=True) dc128 = DeconvLayer(dc256, depth_in=256, depth_out=128, name="dc128", kernel_size=3, acti=True) dc64 = DeconvLayer(dc128, depth_in=128, depth_out=64, name="dc64", kernel_size=3, acti=True) output = ConvLayer(dc64, depth_in=64, depth_out=1, name="conv_out", kernel_size=3, acti=True) return output # Predictions y = model(x) y_image = tf.reshape(y, [-1, img_w, img_h, 1]) tf.summary.image('output', y_image, 6) #Loss loss = tf.reduce_sum(tf.pow(y - y_,2))/(img_w*img_h*img_ch) # MSE loss_summary = tf.summary.scalar("Training_Loss", loss) # Optimizer. with tf.name_scope("train"): train_step = tf.train.AdamOptimizer(learning_rate=learn_rate).minimize(loss) In case you wanna see more details def ConvLayer(input, depth_in, depth_out, name="conv", kernel_size=3, acti=True): with tf.name_scope(name): w = tf.Variable(tf.truncated_normal([kernel_size, kernel_size, depth_in, depth_out], stddev=0.1), name="W") b = tf.Variable(tf.constant(0.1, shape=[depth_out]), name="B") conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME") tf.summary.histogram("weights", w) tf.summary.histogram("biases", b) if (acti==True): act = tf.nn.relu(conv + b) tf.summary.histogram("activations", act) result = act else: result = conv + b result_maxpooled = max_pool(result,2) return result_maxpooled . def DeconvLayer(input, depth_in, depth_out, name="deconv", kernel_size=3, acti=True): with tf.name_scope(name): w = tf.Variable(tf.truncated_normal([kernel_size, kernel_size, depth_out,depth_in], stddev=0.1), name="W") b = tf.Variable(tf.constant(0.1, shape=[depth_out]), name="B") input_shape = tf.shape(input) output_shape = tf.stack([input_shape[0], input_shape[1]*2, input_shape[2]*2, input_shape[3]//2]) deconv = tf.nn.conv2d_transpose(input, w, output_shape, strides=[1, 1, 1, 1], padding='SAME') tf.summary.histogram("weights", w) tf.summary.histogram("biases", b) if (acti==True): act = tf.nn.relu(deconv + b) tf.summary.histogram("activations", act) result = act else: result = deconv + b return result
How to implement CNN (Convolutional Neural Network) on Multiple GPUs? As Quoted from "Training a Model Using Multiple GPU Cards" (Tutorial from Tensorflow) Place an individual model replica on each GPU. Update model parameters synchronously by waiting for all GPUs to finish processing a batch of data. In order to boost performance by understanding dataflow between Main Memory-CPU-GPU have a look at this answer: Why should preprocessing be done on CPU rather than GPU? : https://stackoverflow.com/a/44377741/4190159