2

I have a data series,

df,
           primary
Buy        484
Sell       429
Blanks     130
FX Spot    108
Income     77
FX Forward 2

trying to crate a dataframe with 2 column. first column values should be the index of df second column should have the values of primary in df

 by using,
 filter_df=pd.DataFrame({'contents':df.index, 'values':df.values})
 I get,
 Exception: Data must be 1-dimensional

1 Answer 1

2

Use reset_index with rename_axis for new column name:

filter_df = df.rename_axis('content').reset_index()

Another solution with rename:

filter_df = df.reset_index().rename(columns={'index':'content'})

For DataFrame from constructor need df['primary'] for select column

filter_df=pd.DataFrame({'contents':df.index, 'values':df['primary'].values})

print (filter_df)
      content  primary
0         Buy      484
1        Sell      429
2      Blanks      130
3     FX Spot      108
4      Income       77
5  FX Forward        2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.