Ankit Goel
Ankit Goel

Reputation: 360

Stacking an array of lists in pandas / SFrame stack equivalent in Pandas

Is there a SFrame stack equivalent in pandas dataframes? Pandas' own stack works only with levels whereas I am looking for expanding a single column at the same level as others which contains lists.

Input Dataframe: There are some more columns like user in actual dataframe

+-------+------------------+
| user  |     friends      |
+-------+------------------+
|  1    |     [2, 3, 4]    |
|  2    |      [5, 6]      |
|  3    | [4, 5, 10, None] |
+----- -+------------------+

Output Dataframe:There are some more columns like user in actual dataframe which should get repeated similarly

+------+--------+
| user | friend |
+------+--------+
|  1   |  2     |
|  1   |  3     |
|  1   |  4     |
|  2   |  5     |
|  2   |  6     |
|  3   |  4     |
|  3   |  5     |
|  3   |  10    |
|  3   |  None  |
+------+--------+

Upvotes: 1

Views: 148

Answers (2)

piRSquared
piRSquared

Reputation: 294506

pd.DataFrame.from_items([
    ('user', df.user.values.repeat(df.friends.str.len())),
    ('friends', np.concatenate(df.friends))
])

   user friends
0     1       2
1     1       3
2     1       4
3     2       5
4     2       6
5     3       4
6     3       5
7     3      10
8     3    None

Upvotes: 1

Gayatri
Gayatri

Reputation: 2253

You could do this

data['friend'].apply(pd.Series).stack().reset_index(level=1, drop=True).to_frame('friend').join(data[['user']], how='left')

This would also work if you had more than one column which was similar to "user" column say something like "other column", then you would just do

data['friend'].apply(pd.Series).stack().reset_index(level=1, drop=True).to_frame('friend').join(data[['user',"other column"]], how='left')

Upvotes: 1

Related Questions