Get the name of a pandas DataFrame

前端 未结 5 558
灰色年华
灰色年华 2020-11-27 06:48

How do I get the name of a DataFrame and print it as a string?

Example:

boston (var name assigned to a csv file)

import pandas         


        
5条回答
  •  情歌与酒
    2020-11-27 06:48

    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.

    And Series are:

    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'
    

提交回复
热议问题