Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate pandas dataframe to display desired output

Tags:

python

pandas

I have the following DataFrame structure:

profile_id  user   birthday
123, 124    test1  day1
131, 132    test2  day2

What I need to display is:

profile_id  user   birthday
123        test1   day1 
124        test1   day1
131        test2   day2
132        test2   day2

In the profile_id column I have a couple of ids separated with a comma, and I need to loop through each id.

like image 750
Site Avatar asked Sep 05 '18 14:09

Site


People also ask

How do you set a specific value in a data frame?

You can set cell value of pandas dataframe using df.at[row_label, column_label] = 'Cell Value'. It is the fastest method to set the value of the cell of the pandas dataframe. Dataframe at property of the dataframe allows you to access the single value of the row/column pair using the row and column labels.

Which pandas function allows you to manipulate data and create new variables?

#2 – Apply Function in Pandas It is one of the commonly used Pandas functions for manipulating a pandas dataframe and creating new variables. Pandas Apply function returns some value after passing each row/column of a data frame with some function.


1 Answers

Here's one way to do

In [1127]: dfs = (df.profile_id.str.split(', ', expand=True).stack()
                   .reset_index(name='profile_id'))

In [1128]: df.loc[dfs.level_0].assign(profile_id=dfs.profile_id)
Out[1128]:
  profile_id   user birthday
0        123  test1     day1
0        123  test1     day1
1        124  test2     day2
1        124  test2     day2
like image 60
Zero Avatar answered Sep 21 '22 11:09

Zero