0
votes

I Have one pandas series with multiple index like this image "target","Lastnewjob", "experienceGroup". this is pandas.core.series.series type. I want to convert it to a dataframe(second image) where "experienceGroup" values will be column names and "target","Lastnewjob" remains as columns.

enter image description here

Dataframe that I want to see

enter image description here

Code to get the series by using groupby.

Job=df.groupby(['target','last_new_job'])['experienceGroup'].value_counts()
Job.unstack()

-- Pandas series

Adding more details So that you can create the Job- pandas series actually resulted from groupBy and value_counts()

details={
"experienceGroup":['0-5','6-12','13-19','20 & above','0-5','6-12','13-19','20 & above'],
"last_new_job":[1,'>4',2,"never",4,3,3,4],
"target":[1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0],
"experience":[1,7,15,20,3,8,17,25]  }   
 df5 = pd.DataFrame(details) 
 df5  

To create Pandas series and display it by unstacking- use the below code

Job=df5.groupby(['target','last_new_job'])['experienceGroup'].value_counts()
Job.unstack()
2
pandas.pydata.org/pandas-docs/stable/reference/api/… You can also use reset_index with MultiIndex.mustnot
@mustnot thanks for your suggestion. But i could not do it. I tried those option. I have updated my question. to create DF and pandas series . Could you please give it a second try. Thanks.Ipsita Dash
can you try this? Job.unstack().reset_index() i write below @BLimitless answer comments about your Series.reset_index ValueErrormustnot

2 Answers

1
votes

I couldn't tell if the comment answered the question in enough detail for you, though @mustnot is right you can use reset_index. Here's my code to recreate your dataframe, and then the solution:

arrays = [
   [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1], 
   [1,2,3,4,'>4','never',1,2,3,4,'>4','never']
]

tuples = list(zip(*arrays))
experienceGroup = {
    '0-5': [2127, 535, 152, 87, 87, 1068, 1146, 278, 61, 52, 40, 500],
    '6-12': [2172, 833, 292, 323, 572, 411, 653, 282, 89, 94, 171, 194],
    '13-19': [911, 444, 184, 201, 711, 109, 169, 59, 51, 45, 160, 31],
    '20 & above': [693, 386, 164, 190, 1317, 118, 148, 80, 30, 36, 225, 13],    
}
index = pd.MultiIndex.from_tuples(tuples, names=['target', 'last_new_job'])
df = pd.DataFrame(data=experienceGroup, index=index)
df

Then my data frame looks like your multi index:

enter image description here

To remove the multi-index, all you need to do is:

df.reset_index(inplace=True)
df

Then you get the single index dataframe you're looking for:

enter image description here

If this answers your question please accept so everyone knows this has been answered, and otherwise let us know if you're still struggling so we can help out!

0
votes

**I Have solved the multi Indexing and Categorical Index for column to have the desired results. **

  1. created a pandas.series object by using groupby().Value_counts()

  2. Used pd.series() object .Unstack() to save it as DataFrame.

  3. Obtained data frame has multi index and column as Categorical Index.

  4. Now convert the Categorical column index to list

  5. Now use reset index on the dataframe Codes :

    #step 2
    Job=df5.groupby(['target','last_new_job'])['experienceGroup'].value_counts().unstack() #step 4 Job.columns=Job.columns.tolist() #step 5 Job.reset_index(inplace=True) Job enter image description here