0

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! :)

1
  • Where does the data come from? Can’t you just remove the first row when passing the data to the constructor? Commented Mar 4, 2020 at 21:27

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

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.