Hướng dẫn python print columns list

Hello, readers! In this article, we will be focusing on different ways to print column names in Python.

Nội dung chính

  • First, where do you find columns in Python?
  • 1. Using pandas.dataframe.columns to print column names in Python
  • 2. Using pandas.dataframe.columns.values
  • 3. Python sorted() method to get the column names
  • How do you print columns and rows in Python?
  • How do I show all rows and columns in Python?
  • How do I print the number of columns in Python?
  • How do I print a column in Pandas?

So, let us get started!


First, where do you find columns in Python?

We often come across questions and problem statements wherein we feel the need to deal with data in an excel or csv file i.e. in the form of rows and columns.

Python, as a programming language, provides us with a data structure called ‘DataFrame’ to deal with rows and columns.

A Python DataFrame consists of rows and columns and the Pandas module offers us various functions to manipulate and deal with the data occupied within these rows and columns.

Today, we will be having a look at the various different ways through which we can fetch and display the column header/names of a dataframe or a csv file.

We would be referring the below csv file in the below examples–

Hướng dẫn python print columns list

Dataset-Bank Loan

1. Using pandas.dataframe.columns to print column names in Python

We can use pandas.dataframe.columns variable to print the column tags or headers at ease. Have a look at the below syntax!

Example:

import pandas

file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv")
for col in file.columns:
    print(col)

In this example, we have loaded the csv file into the environment. Further, we have printed the column names through a for loop using dataframe.columns variable.

Output:

age
ed
employ
address
income
debtinc
creddebt
othdebt
default

2. Using pandas.dataframe.columns.values

Python provides us with pandas.dataframe.columns.values to extract the column names from the dataframe or csv file and print them.

Syntax:

Example:

import pandas

file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv")
print(file.columns.values)

So, the data.columns.values gives us a list of column names/headers present in the dataframe.

Output:

['age' 'ed' 'employ' 'address' 'income' 'debtinc' 'creddebt' 'othdebt' 'default']

3. Python sorted() method to get the column names

Python sorted() method can be used to get the list of column names of a dataframe in an ascending order of columns.

Have a look at the below syntax!

Syntax:

Example:

import pandas

file = pandas.read_csv("D:/Edwisor_Project - Loan_Defaulter/bank-loan.csv")
print(sorted(file))

Output:

['address', 'age', 'creddebt', 'debtinc', 'default', 'ed', 'employ', 'income', 'othdebt']

Conclusion

By this, we have come to the end of this topic. Hope this article turns out to be a hack for you in terms of different solutions for a single problem statement.

For more such posts related to Python, Stay tuned and till then, Happy Learning!! 🙂

I'm trying to get user input (x), and then print x rows, and x/2 columns. I cannot get it to work. I keep only getting two columns of data, with duplicate rows. I'm supposed to complete this using a nested for loop that only loops once (per assignment). I have the following code:

x = int(input("Enter a number: "))
val = x//2

for row in range(x):
    for column in range(val):
        print(row, column)

My goal is to have this type of output if the user inputs 4, for example:

1       1       2
2       1       2
3       1       2
4       1       2

But, this is what I'm getting if a user inputs 4:

0 0
0 1
1 0
1 1
2 0
2 1
3 0
3 1

asked Jun 27, 2021 at 22:01

2

This should do the job: (end = some_string replaces the CR/LF by some_string)

x = int(input("Enter a number: "))
val = x//2

for row in range(1,x+1):
    print(row, end = " ")
    for column in range(1,val+1):
        print(column, end = " ")
    print()

answered Jun 27, 2021 at 22:13

zwitschzwitsch

3243 silver badges9 bronze badges

0

Check this one, try to understand the logic. As you are currently learning, I won't explain now, but if you need help, let me know.

x = int(input("Enter a number: "))
val = x//2

for row in range(x):
    print(row+1, end = ' ')
    for column in range(val):
        print(column +1, end = ' ')
    print('\n')

answered Jun 27, 2021 at 22:12

1

This is should yield the desired output.

x = int(input("Enter a number: "))
val = x//2

for row in range(1, x + 1):
    for col in range(1, val + 1):
        print(col, end=' ')
    print()

answered Jun 27, 2021 at 22:15

A.M. DucuA.M. Ducu

8826 silver badges19 bronze badges

How do you print columns and rows in Python?

3 Easy Ways to Print column Names in Python.

Using pandas. dataframe. columns to print column names in Python. ... .

Using pandas. dataframe. columns. ... .

Python sorted() method to get the column names. Python sorted() method can be used to get the list of column names of a dataframe in an ascending order of columns..

How do I show all rows and columns in Python?

Syntax: pd.set_option('display.max_columns', None).

Syntax: pd.reset_option('max_columns').

get_option() – This function is used to get the values, Syntax: pd.get_option(“display.max_columns”).

How do I print the number of columns in Python?

Get the number of columns: len(df. columns)

How do I print a column in Pandas?

To get the column names in Pandas dataframe you can type print(df. columns) given that your dataframe is named “df”.