2016-11-29 1 views
2

ich über jede Spalte einer Datenrahmen am Iterieren und versuchen, log Plots alsMatplotlib: Cant erstellen Log-Plots

cols = in_df.columns 

for col in cols: 
    in_df[col]=in_df[col].dropna() 
    print (in_df[col].values) 
    in_df[col].map(np.log).hist(bins=1000) 
    plt.xlabel(x_label+col) 
    plt.ylabel('Number of customers in train') 
    plt.savefig(save_dir+col+'.png') 
    plt.close() 

aber ich

[2 2 2 ..., 2 2 2] 
in_df[col].map(np.log).hist(bins=1000) 
File "anaconda/envs/kaggle3/lib/python3.5/site-packages/pandas/tools/plotting.py", line 2988, in hist_series 
    ax.hist(values, bins=bins, **kwds) 
    File "anaconda/envs/kaggle3/lib/python3.5/site-packages/matplotlib/__init__.py", line 1819, in inner 
    return func(ax, *args, **kwargs) 
    File "anaconda/envs/kaggle3/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 5985, in hist 
    m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) 
    File "anaconda/envs/kaggle3/lib/python3.5/site-packages/numpy/lib/function_base.py", line 505, in histogram 
    'range parameter must be finite.') 
ValueError: range parameter must be finite. 

Notiz erhalten zu schaffen, dass die folgenden Werke

in_df.col_name.map(np.log).hist(bins=1000) 

Wie auch immer, ich kann diesen Ansatz nicht verwenden, während ich über alle Spalten iteriere. keine Ahnung warum bekomme ich den Fehler?

+2

nimmst du das Protokoll von Null? – piRSquared

+0

hmm, habe nicht darüber nachgedacht. da könnten Nullen drin sein – AbtPst

Antwort

1

Wenn ich über die Nullen richtig bin, ist der einfachste Weg, um Ihr Problem zu beheben, sie fallen zu lassen. Es gibt eine Reihe von Möglichkeiten, dies zu tun. Unten ist eine:

cols = in_df.columns 

for col in cols: 
    in_df[col]=in_df[col].dropna() 
    print (in_df[col].values) 
    # I edited line below 
    in_df[col].replace(0, np.nan).dropna().map(np.log).hist(bins=1000) 
    # added |<------------------------>| 
    plt.xlabel(x_label+col) 
    plt.ylabel('Number of customers in train') 
    plt.savefig(save_dir+col+'.png') 
    plt.close() 
+0

danke! Das war's – AbtPst