Reputation: 9801
In the following example in pandas package of python, when merging two dataframes with duplicate/common indices, these indices are http://pandas.pydata.org/pandas-docs/dev/10min.html#join
Is it possible to merge it like following instead:
key lval rval
0 foo 1,2 4,5
and will it make a difference if the 1,2,4,5s are string?
Upvotes: 0
Views: 962
Reputation: 375375
I don't recommend using this datastructure, I expecte there is a better way depending on what the next stage of your analysis is...
Saying that, here's one way to do it. Take Series of lists for each key:
In [11]: l = left.groupby('key')['lval'].apply(list)
In [12]: l.name = 'lval'
In [13]: l
Out[13]:
key
foo [1, 2]
Name: lval, dtype: object
In [14]: r = right.groupby('key')['rval'].apply(list)
In [15]: r.name = 'rval'
And then concat these into a DataFrame:
In [16]: pd.concat([l, r], axis=1)
Out[16]:
lval rval
key
foo [1, 2] [4, 5]
Upvotes: 2