Python Pandas pivot_table missing column after pivot -
I have followed the data frame The data frame has been created by reading the CSV file. It has a large data set, but for the purpose of this question, I have used 15 lines as an example from the data set. I
user_id contrib_count total_min_length group_space expert_level 0 23720 108 1112696 0 l -2 1 23720 13 442059 1 L-2 2 23720 12 32180 2 L-2 3 23720 2 20177 3 L-2 4 23720 1 1608 10 L-2 5 1265184 71 260186 LG 6 1265184 10 3466 2 LG 7 1265184 1 12081 4 LG 8 513380 112 1049311 L-49953380 1 97 1 L-4 10 513380 113 361980 2 L-4 11 513380 19 1198323 3 L-4 12 513380 2 88301 4 L-4 13 20251 705 17372707 LG 14 20251 103 2327178 1 LG
Expected Result I smoke What I want to do is follow the data frame:
group_space 0 1 2 3 4 5 6 7 8 9 10 specialist_love user_ id 20251 705 103 68 24 18 2 6 Nain nan 5 22 LG 23720 108 13 12 2 Na NN Naa na na na 1 L -2
Once I am doing this, I can use it for the prediction work where expert_level
as the labels data.
By now I have done the following to create the above matrix, but I am unable to get the expert_level
column as shown after the pivot.
This is what I have done:
class group analysis (): def __init __ (self): self.df = none self.filelocation = '~ / Somelocation / x Csv 'def pivot_dataframe (auto): raw_df = pd.read_csv (self.filelocation) self.df = raw_df [(raw_df [' group_space '] <11)] self.df.set_index ([' user_id ',' group_space
By doing this I am getting:
group_space 0 1 2 3 4 5 6 7 8 9 10 User_ id 20251 705 103 68 24 18 2 6 Nain Nain 5 22 23720 108 13 12 2 No NN Naa Naan 1
As you can see that I finally < If the strong> missing column can be found in the "expert_level column, the question is, what is the data that I have with the specialist frame as shown in my" expected result " How do I get above my name?
When you unstack, you only contrib_count a series The expert_level and total_money_liner had already gone to that point.
Instead of setting and unsetting the index, you can only .pivot ()
Then, create a frame in the form of user_id as index and expert_level columns, to get rid of duplicates:
lookup = df.drop_duplicat Es ('user_id') [['user_id', 'expert_level']] lookup.set_index (['user_id'], inplace =
then join your pivot and lookup
result = pivoted.join (lookup)
Edit: If you want to include 'total_min_length' as well, then you can make a second pivot :
pivoted2 = df.pivot ('user_id', 'group_space', 'total_min_length')
>and join three instead of two:
result = pivoted.jonne (lookup) .join (pivoted 2, lsuffix = "_ contrib_count", rsuffix = "_ total_min_length")
meditation Give that Lefix and rasufix are required to separate the columns, because in both points there are 0, 1, 2, 3, 4 and 10 columns with your example data.
Comments
Post a Comment