Python Forum

Full Version: pd.DataFrame question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I hope you are all having a good. Why does the following program cause Python to crash (I'm using the Jupyter notebook):

df2=pd.DataFrame(data=df,index=df['Day of Week'],columns=df['Hour'])

df is a DataFrame already and I want it to have an index of the day of the week variable and columns for the hour variable.
You should check documentation forĀ pandas dataframe. columns parameter accepts list of column names, so you are trying to create dataframe with colums named as values in df.Hour column (with width given by lenght of your original dataframe).

If you just want to use existing column as an index, just use:
df2 = df.set_index('Day of Week', drop=True)[['Hour']]
that will set index values from Day of Week column and deletes Day of Week column. If your original dataframe contains only Hour and Day of Week columns, selecting with [['Hour']] is obsolete.
(Mar-30-2017, 08:17 PM)zivoni Wrote: [ -> ]You should check documentation for pandas dataframe. columns parameter accepts list of column names, so you are trying to create dataframe with colums named as values in df.Hour column (with width given by lenght of your original dataframe). If you just want to use existing column as an index, just use:
 df2 = df.set_index('Day of Week', drop=True)[['Hour']] 
that will set index values from Day of Week column and deletes Day of Week column. If your original dataframe contains only Hour and Day of Week columns, selecting with [['Hour']] is obsolete.


Thank you!