How to Only Read a Subset of Columns of a Dataframe in Python
In this Pandas tutorial, we will learn half dozen methods to get the cavalcade names from Pandas dataframe. One of the nice things about Pandas dataframes is that each cavalcade will accept a name (i.e., the variables in the dataset). Now, we can utilise these names to access specific columns by proper noun without having to know which column number it is.
To access the names of a Pandas dataframe, we tin can the method columns(). For case, if our dataframe is chosen df we only type impress(df.columns) to go all the columns of the Pandas dataframe.
After this, we can piece of work with the columns to access sure columns, rename a column, then on. In the side by side department, earlier learning the methods for getting the cavalcade names of a dataframe, we will import some data to play with.
Importing Information from a CSV File
Get-go, before learning the 6 methods to obtain the column names in Pandas, we need some example data. In this post, we will use Pandas read_csv to import data from a CSV file (from this URL). Now, the commencement stride is, as usual, when working with Pandas to import Pandas as pd.
import pandas equally pd df = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/carData/UN98.csv', index_col=0) df.caput()
Lawmaking language: Python ( python )
It is, of course, also possible to read xlsx files using Pandas read_excel method. Another method to go our data into Python is to convert a dictionary to a Pandas dataframe. After you have found the answer on the question "How do I get column names in Pandas?" you volition learn how to get cavalcade names in half-dozen different means.
How practice I become cavalcade names in Pandas?
To get the column names in Pandas dataframe you can type <code>print(df.columns)</code> given that your dataframe is named "df". There are, of course, at least 5 other options for getting the cavalcade names of your dataframe (east.g., <code>sorted(df)</code>).
Pandas How to Go the Cavalcade Names from the Dataframe:
Now, we are ready to learn how we can get all the names using six unlike methods. Showtime, we use the DataFrame.columns method to print all names:
1. Get the Column Names Using the columns() Method
Now, 1 of the simplest methods to become all the columns from a Pandas dataframe is, of course, using the columns method and press information technology. In the code chunk below, nosotros are doing exactly this.
Lawmaking linguistic communication: Python ( python )
impress(df.columns)
Correct, the columns method will get the labels of the dataframe. That is, when we use impress we will print column names (i.e., the labels). Here's the consequence of the above code:
In the next example, we are going to use the keys() method to print all the names in the dataframe:
2. Using the keys() Method
Second, nosotros can get the exact same result past using the keys() method. That is, we volition get the cavalcade names by the post-obit code as well.
# Dataframe show all columns print(df.keys())
Code language: Python ( python )
In the next case, nosotros volition iterate over the DataFrame.columns to print each name on a split line.
three. By Iterating of the Columns
In the third method, we will simply iterate over the columns to get the column names. Equally yous may detect, we are again using the columns method.
# Get all names for col_name in df.columns: print(col_name)
Code language: Python ( python )
In the adjacent example, we will become all the names using the list() method together with the df.columns method.
4. Using list() to Print the Names as a list
In the 4th method, on the other paw, we are going to use the list() method to print the cavalcade names as a list.
# Impress the columns as listing print(list(df.columns))
Code language: Python ( python )
Another option, which we will encounter in the next case, is the tolist() method.
5. Using tolist() to Print the Names as a List
At present, we tin utilise the values method, as well, to get the columns from Pandas dataframe. If we likewise utilize the tolist() method, nosotros will get a list, as well.
# Show all columns equally list print(df.columns.values.tolist())
Lawmaking language: Python ( python )
vi. Using sorted() to Get an Ordered Listing
Now, in the terminal, and 6th, method to print the names, nosotros will employ sorted() to get the columns from a Pandas dataframe in alphabetic lodge:
# Dataframe show all columns sorted list sorted(df)
Code language: Python ( python )
Equally previously mentioned, when using sorted we volition get this ordered list of column names:
How to Go Values by Cavalcade Name:
At present, that we know the column names of our dataframe nosotros can admission one column (or many). Here'southward how we become the values from one column:
impress(df['tfr'].values)
Code linguistic communication: Python ( python )
If nosotros, on the other hand, want to access more than than i column we add a list: df[['tfr', 'region']]
How to Rename a Cavalcade
In the final example, what we tin do when we know the column names of a Pandas dataframe is to rename a cavalcade.
df.rename(columns={'tfr': 'TFR'})
Code language: Python ( python )
Note, if nosotros want to save the changed proper noun to our dataframe we can add together the inplace=True, to the lawmaking to a higher place. In the video below, you lot will learn how to utilize the inplace parameter, also every bit all the other things from this post. In a more contempo post, you will acquire all y'all need about renaming columns in Pandas dataframe.
Conclusion: Getting all the Column Names with Pandas
Now, in this post, nosotros have learned how to become the column names from a Pandas dataframe. Specifically, we learned why and when this tin can be useful, 6 different methods to access the column names, and very briefly what we can do when nosotros know the column names. Finally, here's the Jupyter Notebook with all the example code.
Source: https://www.marsja.se/how-to-get-the-column-names-from-a-pandas-dataframe-print-and-list/
0 Response to "How to Only Read a Subset of Columns of a Dataframe in Python"
Post a Comment