简体   繁体   中英

How to convert pandas.core.series.Series object with Multiple index to a pandas Dataframe with all columns filled?

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.

在此处输入图像描述

Dataframe that I want to see

在此处输入图像描述

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()

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:

在此处输入图像描述

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:

在此处输入图像描述

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!

**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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM