1

I have created a sort of matrix/data-table in python from multiple arrays containing various kinds of data, which looks like this in the variable view:

phones  starts  mids

sil 308000.0    308195.0

DH  308390.0    308410.0

AH0 308430.0    308445.0

B   308460.0    308525.0

These are three lists that I have combined using numpy.columnstack(). I am trying to output this table/matrix in the same format/structure but as a csv using numpy.savetxt. Is this possible given the different variable types in each row/column?

When I try to run :

np.savetxt('blue.txt', np.column_stack((phones, phone_starts, phone_mids)), ',', fmt="%s %f")

I get the error:

File "<__array_function__ internals>", line 4, in savetxt
TypeError: _savetxt_dispatcher() got multiple values for argument 'fmt'

I have also tried without the 'fmt' option and have gotten a similar error.

Any ideas?

4
  • Look at/show the result of the column_stack. What is its dtype and shape? My guess it is 2d with a string dtype. (with 3 columns). In which case, I'd try fmt='%s', savetxt writes a fmt % tuple(row) for each row of the array. Commented Mar 29, 2020 at 3:13
  • The input order for savetxt is filename, X, fmt, delimiter, .... Use delimiter=',', fmt='%s'. Commented Mar 29, 2020 at 3:18
  • The type is 'Array of str256' of size (939,3) Commented Mar 29, 2020 at 4:40
  • actually size is (939,2) Commented Mar 29, 2020 at 4:42

1 Answer 1

1

Well, do you want to save np arrays to csv with labels? that is the type of work for which pandas were created.

import numpy as np
import pandas as pd

data = {
"phone_starts": np.array([308000.0, 308390.0, 308430.0, 308460.0]),
"phone_mids": np.array([308195.0, 308410.0, 308445.0, 308525.0])
   }

df = pd.DataFrame(data, index=["sil", "DH", "AH0", "B"])
df.index.name = "Phone"
df.to_csv("blue.csv")
Sign up to request clarification or add additional context in comments.

3 Comments

Hello thank you very much for responding. I got the sense that pandas might be necessary/more appropriate. i do want to save an array with labels so I will try implementing the code you suggested.
Am I able to create the data frame from the lists stored in variables or do I have to type them manuall? Because the portion above is only just a small part of the whole data
yes, for example, df = pd.DataFrame(list_arrays, columns=list_index, index=list_columns), if you create a list of np.arrays as data, I think DataFrame will infer it as rows, but it's possible to change that after. df = df.T

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.