2

I have an array that looks like this:

{'loc.1': array([  1,2,3,4,7,5,6]),'loc.2': array([  3,4,3,7,7,8,6]),'loc.3': array([  1,4,3,1,7,8,6]).....} 

After a = pd.DataFrame(array) it looks like this:

loc.1    loc.2  loc.3
1        3      1
2        4      4
3        3      3
4        7      1
7        7      7
5        8      8
6        6      6

This is however what I want:

Col1.    Col.2 
loc.1    1,2,3,4,7,5,6
loc.2    3,4,3,7,7,8,6
loc.3    1,4,3,1,7,8,6 

I need it in this particular format as I wish to concatenate subsequently with another table. Pandas would be my preferred solution..

Thank you, and apologies if this is a silly question.

4 Answers 4

3

First in dictionary comprehension need join values.

Then for Series use:

a = pd.Series({k:','.join(v.astype(str)) for k, v in array.items()})
print (a)
loc.1    1,2,3,4,7,5,6
loc.2    3,4,3,7,7,8,6
loc.3    1,4,3,1,7,8,6
dtype: object

And for DataFrame:

d = {k:','.join(v.astype(str)) for k, v in array.items()}
a = pd.DataFrame({'a': list(d.keys()), 'b': list(d.values())})

Alternative solution is create tuples:

L = [(k, ','.join(v.astype(str))) for k, v in array.items()]
a = pd.DataFrame(L, columns=['a','b'])

print (a)
       a              b
0  loc.1  1,2,3,4,7,5,6
1  loc.2  3,4,3,7,7,8,6
2  loc.3  1,4,3,1,7,8,6

If need arrays in column remove join with cast to strings:

L = [(k, v) for k, v in array.items()]
a = pd.DataFrame(L, columns=['a','b'])
print (a)
       a                      b
0  loc.1  [1, 2, 3, 4, 7, 5, 6]
1  loc.2  [3, 4, 3, 7, 7, 8, 6]
2  loc.3  [1, 4, 3, 1, 7, 8, 6]
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you - how can I remove the index on the left-hand side and make a the index?
You can use a.set_index('a')
Or a = pd.Series({k:','.join(v.astype(str)) for k, v in array.items()}).to_frame('b')
1

A couple of options depending on what format you require:

d = {'loc.1': np.array([  1,2,3,4,7,5,6]),
     'loc.2': np.array([  3,4,3,7,7,8,6]),
     'loc.3': np.array([  1,4,3,1,7,8,6])} 

res1 = pd.DataFrame([[x] for x in d.values()], index=d.keys())

#                            0
# loc.1  [1, 2, 3, 4, 7, 5, 6]
# loc.2  [3, 4, 3, 7, 7, 8, 6]
# loc.3  [1, 4, 3, 1, 7, 8, 6]

res2 = pd.DataFrame([', '.join(list(map(str, x))) for x in d.values()], index=d.keys())

#                          0
# loc.1  1, 2, 3, 4, 7, 5, 6
# loc.2  3, 4, 3, 7, 7, 8, 6
# loc.3  1, 4, 3, 1, 7, 8, 6

Comments

1
a = {'loc.1': [1,2,3,4,7,5,6],'loc.2': [3,4,3,7,7,8,6],'loc.3': [1,4,3,1,7,8,6]}
import pandas as pd
df = pd.DataFrame(a).transpose()
df['lists'] = df[[0,1,2,3,4,5,6]].values.tolist()
df = df['lists']

Output:

loc.1    [1, 2, 3, 4, 7, 5, 6]
loc.2    [3, 4, 3, 7, 7, 8, 6]
loc.3    [1, 4, 3, 1, 7, 8, 6]
Name: lists, dtype: object

Comments

1

You can using stack with groupby

df.stack().astype(str).groupby(level=1).apply(','.join)
Out[738]: 
loc.1    1,2,3,4,7,5,6
loc.2    3,4,3,7,7,8,6
loc.3    1,4,3,1,7,8,6
dtype: object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.