How to print out different headings on each row Python

python print column alignment
python formatting output into columns
python print columns list
python print table align
python print format
python print align left
different ways to print in python
how to align text in python

Currently I have some data:

EXAMPLE_DATA = [
    ['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
    ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
    ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]

I have a function called 'example_func' that calls EXAMPLE_DATA[1] e.g: the 2nd row of the data

I then used the code:

def display_data( example_func ):

    for row in example_func:
        print(row)

This gives the following output:

18:42:11
61
153.9615
0.8
Mixed
None

I want the following output to be:

Time: 18:42:11
Age: 61
Height: 153.9615
Ethnicity: Mixed

However, I want to set the headings in my code, and don't want to use the headings from the EXAMPLE_DATA.

As you'll have noticed, I also don't want to display 'Width' or 'Religion' in my final output.

If you need any more information please let me know.

Here is a function that does the job:

def display_data(example_func):

    headings = ['Time', 'Age', 'Height', 'Ethnicity']  # Your headings
    rows = [0, 1, 2, 4]  # The corresponding rows

    for heading, row in zip(headings, rows):  # extracts each heading and the corresponding row
        print(heading + ': ' + example_func[row])

Python: Real World Machine Learning, print ('Total rows: %i' % (n+1)) print ('Header: %s' % ', '.join(header)) print ('​Sample DictReader that produces a Python dictionary as an output (which is  If I have a list defined as list=['Ford','Mustang','1966','red'] and try to print it my output would look like: Ford Mustang 1966 red. But how can I achieve to have a heading so the output would look like: Brand Modell Year Color Ford Mustang 1966 Red That would look much more professional.

Not exactly sure I understand all of your question, but here's a guess:

from collections import namedtuple

EXAMPLE_DATA = [
    ['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
    ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
    ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for row in example_func[1:]:
        print('Time: {time}\n'
              'Age: {age}\n'
              'Height: {height}\n'
              'Ethnicity: {ethnicity}\n'.format(**Record(*row)._asdict()))

display_data(EXAMPLE_DATA)

You could write it a little more concisely using f-strings in Python 3.6+.

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for rec in (Record(*row) for row in example_func[1:]):
        print(f'Time: {rec.time}\n'
              f'Age: {rec.age}\n'
              f'Height: {rec.height}\n'
              f'Ethnicity: {rec.ethnicity}\n')

Large Scale Machine Learning with Python, print ('Total rows: %i' % (n+1)) print ('Header: %s' % ', '.join(header)) print ('​Sample DictReader that produces a Python dictionary as an output (which is  The code then looped over each of the 12 items in our data variable. If we are dealing with our first item (i.e., i = 0), we tell Python that we want to print a line of dashes, then print our headers, followed by another line of dashes. Notice how we specify the number of spaces we want for each header as well as the alignment of the text.

use Pandas for your problem. Pandas will automatically handle the data as your requested format. you can modify the output as your wish.

Data Wrangling with Python: Tips and Tools to Make Your Life Easier, Zips each row (now exactly matched with header and data) and adds it to a new array, zipped data. We can now print out a row of our new dataset and see if we  This article shows you how you can print large columnar data in python in a readable way. To explain clearly, I am using the NYC Property sales data , which has a total of 21 columns. This is what happens if you have a Pandas DataFrame with many columns and try to print it out with a regular print statement:

Note: the following only works if you pass the whole table instead of one row. Oversight on my part, sorry. It's significantly more elegant this way. If you really want it to only work on one row, example code is at the end.

You could make a dictionary, mapping the headings you want to the index they have in EXAMPLE_DATA:

 HEADINGS = {'Time':0, 'Age':1, 'Height':2, 'Ethnicity':4}

With this, you can just run through the keys and display the value at the corresponding index.

def display_data( example_func, headings ):
    for row in example_func:
        for key in headings.keys():
            print(key + ": " + row[headings[key]]

However, if you want absolute efficiency, you can calculate headings.keys() once and just use it several times. It doesn't matter.

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in example_func():
        for key in keys:
            print(key + ": " + row[headings[key]]

One-row example code:

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in [example_func()]:
        for key in keys:
            print(key + ": " + row[headings[key]]

Head First Python: A Brain-Friendly Guide, The column headings from the first line are loaded into a list called column_headings. The rest ofthe data (all the rows of times) are loaded into a dictionary called Use the row label num_cols = len(column_headings) print(​num_cols, end='  Get the list of column headers or column name in python pandas In this tutorial we will learn how to get the list of column headers or column name in python pandas using list() function. Lets see with an example

Take control of your Python print() statements: part 3, However, some types of data tend to be better presented in a nicely tell Python that we want to print a line of dashes, then print our headers,  Note: print() was a major addition to Python 3, in which it replaced the old print statement available in Python 2. There were a number of good reasons for that, as you’ll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.

Python: Read a CSV file line by line with or without header , Python has a csv module, which provides two different classes to read In initially saved the header row in a separate variable and printed that  The row with index 3 is not included in the extract because that’s how the slicing syntax works. Note also that row with index 1 is the second row. Row with index 2 is the third row and so on. If you’re wondering, the first row of the dataframe has an index of 0. That’s just how indexing works in Python and pandas.

Print row and column headings - Excel - Office support, on workbook. Follow these steps to print the column and row headings for a worksheet: If you want to do this for multiple worksheets, you can group them. df.head(n) #This will Print the top ’n’ entries from the data frame. In your case, put n = 10. df.tail(n) #This will Print the bottom ’n’ entries from the data frame. In your case, put n = 10. If you want to print Column Names, use the for loop as below - for i in df.columns: print(i)

Comments
  • Thank you! Everything works other than i'd like it only to display 'Time', 'Age', 'Height' and 'Ethnicity', how can i do this?
  • OK...give me a minute.