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.
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.
#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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With