0
我有一個循環來生成大熊貓DF每列的圖。我使用Ipython,但是這些圖都顯示在循環結尾,而不是顯示在我想要根據代碼顯示它們的地方。ipython強制大熊貓繪製
我該如何強制ipython/pandas在我有'plot'功能的精確點上顯示cols?
def explore(file, sep=";", top = 5, k='Code Agence'):
"""
"""
%matplotlib inline
import time
import matplotlib.pyplot as plt
import pandas as pd
import time
import sys
dataframes_top = []
start = time.time()
#print "Exploring :", get_file_name(file), "with %s lines"%(top)
to_explore = pd.read_csv(file, sep=";", error_bad_lines=False)
cols = to_explore.columns
i = -1
for col in cols:
i +=1
serie = to_explore[col]
try:
print"plotting %s"%(col)
serie.plot().show()
time.sleep(2)
except Exception as e:
"plotting issue :%s"%(e)
#serie.index = index
null = serie.isnull()
not_null = len([x for x in null if not x])
r = not_null/len(serie)
s = serie.value_counts()#return value as index, count as value
pct_top = s.values[:top]/not_null
serie_top_n = pd.Series(s.values[:top],index=s.index[:top])
local_df = pd.DataFrame()
local_df[col]=serie_top_n
local_df['pct']=pct_top
somme = local_df['pct'].sum()
pct_2_top= s.values[:top*2]/not_null
serie_2_top_n = pd.Series(s.values[:top*2],index=s.index[:top*2])
local_df_2_top = pd.DataFrame()
local_df_2_top[col]=serie_2_top_n
local_df_2_top['pct']=pct_2_top
somme_2_top = local_df_2_top['pct'].sum()
print
print "%s : [col %s = %s ] "%(get_file_name(file), i,col)
print
print "%.2f"%(r), " pct not null"
print "%.2f pct on the first %s "%(somme, top)
print "%.2f pct on the first %s "%(somme_2_top, 2*top)
print "plot :"
print pd.DataFrame(serie.describe()).T
print
print local_df.T
print "plot :"
local_df.plot()
print "="*100
dataframes_top.append(local_df)
elapsed = time.time()-start
print "="*20, elapsed, "for %s lines"%(len(serie)),"="*20
sys.stdout.flush()