Charlie Vagg
Charlie Vagg

Reputation: 97

Numpy array to Pandas data frame formatting

Sorry if this has already been answered somewhere!

I am trying to format an array in numpy to a data frame in pandas, which I have done like so:

# array
a = [[' ' '0' 'A' 'T' 'G']
    ['0' 0 0 0 0]
    ['G' 0 -3 -3 5]
    ['G' 0 -3 -6 2]
    ['A' 0 5 0 -3]
    ['A' 0 5 2 -3]
    ['T' 0 0 10 5]
    ['G' 0 -3 5 15]]

# Output data frame using pandas

   0  1   2   3   4
0     0   A   T   G
1  0  0   0   0   0
2  G  0  -3  -3   5
3  G  0  -3  -6   2
4  A  0   5   0  -3
5  A  0   5   2  -3
6  T  0   0  10   5
7  G  0  -3   5  15

# Output I want

   0   A   T   G
0  0   0   0   0
G  0  -3  -3   5
G  0  -3  -6   2
A  0   5   0  -3
A  0   5   2  -3
T  0   0  10   5
G  0  -3   5  15

Any advice on how to do this would be appreciated! :)

Upvotes: 0

Views: 133

Answers (1)

DYZ
DYZ

Reputation: 57105

Declare the first row to be column names and the first column to be row names:

df = pd.DataFrame(data=a[1:], columns=a[0]).set_index(' ')
df.index.name = None
#   0  A  T  G
#0  0  0  0  0
#G  0 -3 -3  5
#G  0 -3 -6  2
#A  0  5  0 -3

Upvotes: 3

Related Questions