Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7709669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:55:12+00:00 2026-06-01T00:55:12+00:00

I want to save a 2D array to a CSV file with row and

  • 0

I want to save a 2D array to a CSV file with row and column “header” information (like a table). I know that I could use the header argument to numpy.savetxt to save the column names, but is there any easy way to also include some other array (or list) as the first column of data (like row titles)?

Below is an example of how I currently do it. Is there a better way to include those row titles, perhaps some trick with savetxt I’m unaware of?

import csv
import numpy as np

data = np.arange(12).reshape(3,4)
# Add a '' for the first column because the row titles go there...
cols = ['', 'col1', 'col2', 'col3', 'col4']
rows = ['row1', 'row2', 'row3']

with open('test.csv', 'wb') as f:
   writer = csv.writer(f)
   writer.writerow(cols)
   for row_title, data_row in zip(rows, data):
      writer.writerow([row_title] + data_row.tolist())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:55:14+00:00Added an answer on June 1, 2026 at 12:55 am

    Maybe you’d prefer to do something like this:

    # Column of row titles
    rows = np.array(['row1', 'row2', 'row3'], dtype='|S20')[:, np.newaxis]
    with open('test.csv', 'w') as f:
        np.savetxt(f, np.hstack((rows, data)), delimiter=', ', fmt='%s')
    

    This is implicitly converting data to an array of strings, and takes about 200 ms for every million items in my computer.

    The dtype '|S20' means strings of twenty characters. If it’s too low, your numbers will get chopped:

    >>> np.asarray([123], dtype='|S2')
    array(['12'], 
      dtype='|S2')
    

    Another option, that from my limited testing is slower, but gives you a lot more control and doesn’t have the chopping issue would be using np.char.mod, like

    # Column of row titles
    rows = np.array(['row1', 'row2', 'row3'])[:, np.newaxis]
    str_data = np.char.mod("%10.6f", data)
    with open('test.csv', 'w') as f:
        np.savetxt(f, np.hstack((rows, str_data)), delimiter=', ', fmt='%s')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects that I want to save as a file
I want to save an array/object into keychain but it looks like keychain only
I want to use numpy.savetxt() to save an array of complex numbers to a
I have a byte array that I want to save it as a bitmap
I want to save current path information in an Array and one field is
I want to save to disk an array of objects as plist file .
I want to save a multidimensional array to a file. A struct for example:
i want to save kingarray[x1+1,y1-1],king array[x1+1,y1],etc in an array(ways that king in chess game
I want to save all but some excluded fields. I know that I can
Is it possible to save a Sprite into an Array? What i want is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.