Save structured numpy array using np.savetxt with header
I have a structure array in the form of
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?
Thank you in advance.
python numpy save structured-array
add a comment |
I have a structure array in the form of
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?
Thank you in advance.
python numpy save structured-array
output.dtype.namesis a list of those field names. You could use that format a header line, e.g.' '.join(output.dtype.names)
– hpaulj
Jun 25 '18 at 16:12
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
The header line doesn't affect the layout of the data. That's handled by thefmtas specified in the answer. Tosavetxt, the header is just a string it writes along with the comment character.
– hpaulj
Jun 26 '18 at 16:18
add a comment |
I have a structure array in the form of
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?
Thank you in advance.
python numpy save structured-array
I have a structure array in the form of
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?
Thank you in advance.
python numpy save structured-array
python numpy save structured-array
asked Jun 25 '18 at 15:51
somebodyzhsomebodyzh
61
61
output.dtype.namesis a list of those field names. You could use that format a header line, e.g.' '.join(output.dtype.names)
– hpaulj
Jun 25 '18 at 16:12
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
The header line doesn't affect the layout of the data. That's handled by thefmtas specified in the answer. Tosavetxt, the header is just a string it writes along with the comment character.
– hpaulj
Jun 26 '18 at 16:18
add a comment |
output.dtype.namesis a list of those field names. You could use that format a header line, e.g.' '.join(output.dtype.names)
– hpaulj
Jun 25 '18 at 16:12
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
The header line doesn't affect the layout of the data. That's handled by thefmtas specified in the answer. Tosavetxt, the header is just a string it writes along with the comment character.
– hpaulj
Jun 26 '18 at 16:18
output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)– hpaulj
Jun 25 '18 at 16:12
output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)– hpaulj
Jun 25 '18 at 16:12
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
The header line doesn't affect the layout of the data. That's handled by the
fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.– hpaulj
Jun 26 '18 at 16:18
The header line doesn't affect the layout of the data. That's handled by the
fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.– hpaulj
Jun 26 '18 at 16:18
add a comment |
2 Answers
2
active
oldest
votes
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
add a comment |
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51027447%2fsave-structured-numpy-array-using-np-savetxt-with-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
add a comment |
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
add a comment |
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
Below is sample code:
output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')
As documented here.
answered Jun 25 '18 at 16:03
chifu linchifu lin
665
665
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
add a comment |
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?
– somebodyzh
Jun 26 '18 at 16:04
add a comment |
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
add a comment |
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
add a comment |
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
You could try a solution similar to this SO answer to pivot the data
dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))
Where you map list over the recarray tuples, and then vertically stack them.
Then you can do the following...
names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))
edited Nov 14 '18 at 8:57
answered Nov 13 '18 at 11:52
ryanjdillonryanjdillon
7,04864974
7,04864974
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51027447%2fsave-structured-numpy-array-using-np-savetxt-with-header%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
output.dtype.namesis a list of those field names. You could use that format a header line, e.g.' '.join(output.dtype.names)– hpaulj
Jun 25 '18 at 16:12
@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?
– somebodyzh
Jun 26 '18 at 16:08
The header line doesn't affect the layout of the data. That's handled by the
fmtas specified in the answer. Tosavetxt, the header is just a string it writes along with the comment character.– hpaulj
Jun 26 '18 at 16:18