Gets a list of column headers from pandas DataFrame

This translation from: the Get List column headers from PANDAS DataFrame

I want to get a list of the column headers from a pandas DataFrame. I want to get a list of column headings from pandas DataFrame. DataFrame by Will Come from the User at The the INPUT SO MANY How the I know the Columns by Will not there by Will BE or the What They Called by Will BE. DataFrame input from the user, so I do not know how many columns or what they will be called.

For example, if I'm given a DataFrame like this: For example, if you give me this DataFrame:

>>> my_dataframe
    y  gdp  cap
0   1    2    5
1   2    3    9
2   8    7    2
3   3    4    7
4   6    7    7
5   4    8    3
6   8    2    8
7   9    9   10
8   6    6    4
9  10   10    7

I would want to get a list like this: I want a list like this:

>>> header_list
['y', 'gdp', 'cap']

#1st Floor

Reference: https://stackoom.com/question/1JkPS/ get a list of column headers from pandas-DataFrame


#2nd Floor

By my_dataframe.columns.


#3rd floor

You can get the values as a list by doing: You can do the following to get the value in list form:

list(my_dataframe.columns.values)

Simply use you CAN Also: (AS Shown in Ed Chum's answer ): You can also simply use :( as Ed Chum answers shown ):

list(my_dataframe)

#4th floor

n = []
for i in my_dataframe.columns:
    n.append(i)
print n

#5th Floor

There is a built in method which is the most performant: a built-in method is the most effective:

my_dataframe.columns.values.tolist()

.columnsAN Index returns A, .columns.valuesreturns A AN Array A Helper has and the this function .tolistto return A List. .columnsreturns an index, .columns.valuesreturns an array, it has the help function .tolistto return the list.

Important not AS IS Performance IF you to, IndexObjects the DEFINE A .tolist()Method, that you CAN Call Directly: If the performance is not so important to you, the Indexobject is to define a .tolist()method, you can call this method directly:

my_dataframe.columns.tolist()

The difference in performance is obvious: the performance difference is obvious:

%timeit df.columns.tolist()
16.7 µs ± 317 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit df.columns.values.tolist()
1.24 µs ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Those Typing the WHO hate the For, you CAN the Just Call listON df, SO AS: For those who hate typing, you can dfon the call listas follows:

list(df)

#6th floor

A DataFrame Follows The dict for iterating through the over-like Convention of The "Keys" Objects of The. DataFrame follow a similar convention dict that iterates over object "key."

my_dataframe.keys()

Method Object - A List of the Create Keys / Columns to_list()and Pythonic Way list creation key / column - object methods to_list()and to_list()methods

my_dataframe.keys().to_list()
list(my_dataframe.keys())

The Iteration Basic ON A DataFrame returns A column Labels DataFrame of basic iterative return column labels

[column for column in my_dataframe]

Do not convert a DataFrame into a list , just to get the column labels. Do not just to get the column labels will DataFrame into a list. Do not stop thinking while looking for convenient code samples. When looking for convenient sample code, please do not stop thinking.

xlarge = pd.DataFrame(np.arange(100000000).reshape(10000,10000))
list(xlarge) #compute time and memory consumption depend on dataframe size - O(N)
list(xlarge.keys()) #constant time operation - O(1)
Original articles published 0 · won praise 73 · views 550 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105328745