Pandas DataFrames
A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.
Example
Create a simple Pandas DataFrame:
data = «calories»: [420, 380, 390],
«duration»: [50, 40, 45]>
#load data into a DataFrame object:
df = pd.DataFrame(data)
Result
calories duration 0 420 50 1 380 40 2 390 45
Locate Row
As you can see from the result above, the DataFrame is like a table with rows and columns.
Pandas use the loc attribute to return one or more specified row(s)
Example
Result
calories 420 duration 50 Name: 0, dtype: int64
Note: This example returns a Pandas Series.
Example
Result
calories duration 0 420 50 1 380 40
Note: When using [] , the result is a Pandas DataFrame.
Get Certified!
Complete the Pandas modules, do the exercises, take the exam, and you will become w3schools certified!
Named Indexes
With the index argument, you can name your own indexes.
Example
Add a list of names to give each row a name:
data = «calories»: [420, 380, 390],
«duration»: [50, 40, 45]>
df = pd.DataFrame(data, index = [«day1», «day2», «day3»])
Result
calories duration day1 420 50 day2 380 40 day3 390 45
Locate Named Indexes
Use the named index in the loc attribute to return the specified row(s).
Example
Result
calories 380 duration 40 Name: day2, dtype: int64
Load Files Into a DataFrame
If your data sets are stored in a file, Pandas can load them into a DataFrame.
Example
Load a comma separated file (CSV file) into a DataFrame:
You will learn more about importing files in the next chapters.
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
[python] Get the name of a pandas DataFrame
How do I get the name of a DataFrame and print it as a string?
boston (var name assigned to a csv file)
import pandas as pd boston = pd.read_csv('boston.csv') print('The winner is team A based on the %s table.) % boston
The answer is
You can name the dataframe with the following, and then call the name wherever you like:
import pandas as pd df = pd.DataFrame( data=np.ones([4,4]) ) df.name = 'Ones' print df.name >>> Ones
Sometimes df.name doesn’t work.
you might get an error message:
‘DataFrame’ object has no attribute ‘name’
def get_df_name(df): name =[x for x in globals() if globals()[x] is df][0] return name
In many situations, a custom attribute attached to a pd.DataFrame object is not necessary. In addition, note that pandas -object attributes may not serialize. So pickling will lose this data.
Instead, consider creating a dictionary with appropriately named keys and access the dataframe via dfs[‘some_label’] .
From here what I understand DataFrames are:
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects.
Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.).
Series have a name attribute which can be accessed like so:
In [27]: s = pd.Series(np.random.randn(5), name='something') In [28]: s Out[28]: 0 0.541 1 -1.175 2 0.129 3 0.043 4 -0.429 Name: something, dtype: float64 In [29]: s.name Out[29]: 'something'
EDIT: Based on OP’s comments, I think OP was looking for something like:
>>> df = pd.DataFrame(. ) >>> df.name = 'df' # making a custom attribute that DataFrame doesn't intrinsically have >>> print(df.name) 'df'
Here is a sample function: ‘df.name = file` : Sixth line in the code below
def df_list(): filename_list = current_stage_files(PATH) df_list = [] for file in filename_list: df = pd.read_csv(PATH+file) df.name = file df_list.append(df) return df_list
pandas.Series.name#
The name of a Series becomes its index or column name if it is used to form a DataFrame. It is also used whenever displaying the Series using the interpreter.
Returns label (hashable object)
The name of the Series, also the column name if part of a DataFrame.
Sets the Series name when given a scalar input.
Corresponding Index property.
The Series name can be set initially when calling the constructor.
>>> s = pd.Series([1, 2, 3], dtype=np.int64, name='Numbers') >>> s 0 1 1 2 2 3 Name: Numbers, dtype: int64 >>> s.name = "Integers" >>> s 0 1 1 2 2 3 Name: Integers, dtype: int64
The name of a Series within a DataFrame is its column name.
>>> df = pd.DataFrame([[1, 2], [3, 4], [5, 6]], . columns=["Odd Numbers", "Even Numbers"]) >>> df Odd Numbers Even Numbers 0 1 2 1 3 4 2 5 6 >>> df["Even Numbers"].name 'Even Numbers'