Note that there are some explanatory texts on larger screens.

plurals
  1. POPandas - How to pass column name in function
    primarykey
    data
    text
    <p>I might be missing something very simple here but I have a function that plots histograms for a certain column from a Pandas dataframe in certain time spans. In my function I want to pass the column name as an argument. This is what I have so far.</p> <pre><code>def make_hist(data,column_name): start=datetime(2013,11,1) end=datetime(2013,11,30) rng=pd.date_range(start,end) start2=datetime(2013,10,1) end2=datetime(2013,10,30) rng2=pd.date_range(start2,end2) start3=datetime(2013,9,1) end3=datetime(2013,9,30) rng3=pd.date_range(start3,end3) start4=datetime(2013,8,1) end4=datetime(2013,8,30) rng4=pd.date_range(start4,end4) start5=datetime(2013,7,1) end5=datetime(2013,7,30) rng5=pd.date_range(start5,end5) start6=datetime(2013,12,1) end6=datetime(2013,12,30) rng6=pd.date_range(start6,end6) plt.subplot(611) data.ix[rng6].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('December') plt.ylim(0,300) plt.xlim(0,300) plt.subplot(612) data.ix[rng].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('November') plt.ylim(0,300) plt.xlim(0,300) plt.subplot(613) data.ix[rng2].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('October') plt.ylim(0,300) plt.xlim(0,300) plt.subplot(614) data.ix[rng3].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('September') plt.ylim(0,300) plt.xlim(0,300) plt.subplot(615) data.ix[rng4].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('August') plt.ylim(0,300) plt.xlim(0,300) plt.subplot(616) data.ix[rng5].column_name.hist(bins=[0,30,60,90,120,150,180,210,240,270,300]).plot() plt.title('July') plt.ylim(0,300) plt.xlim(0,300) plt.show() </code></pre> <p>The problem is that I do not know what data type the argument <code>column_name</code> should be. It cannot be a string and it cannot just be a variable because I do not know how I would define it. I feel like there is a simple way to do this that I am just not seeing.</p> <p>EDIT: adding a description of the dataframe. The output of <code>data.dtypes</code> is </p> <pre><code>account int64 first_active object datelatest_inactive datetime64[ns] days_active int64 dtype: object </code></pre> <p>and my index is on the <code>datelatest_inactive</code> column</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload