简体   繁体   中英

To specify column names, why same parameter name is used in `pd.read_csv` and `pd.DataFrame`?

While working with pandas, I have encountered that the two most common ways of creating a new data-Frame using pandas are as follows;

1. pandas.read_csv() Type: < class 'pandas.core.frame.DataFrame' >

2. pandas.DataFrame() Type: < class 'pandas.core.frame.DataFrame' >

Both use different parameters for taking a list of column names as input.


Syntax:

  • For pd.read_csv is as given;

     pandas.read_csv(filepath, sep=',', delimiter=None, header='infer', names=None)
  • For pd.DataFrame is as given;

     pd.DataFrame(data=None, index=None, columns=None)

Should I prefer with changing the parameter "names" to "columns" in pandas.read_csv() . Does it make any sense?


After that again, while saving data-frames into a CSV-file using df.to_csv , we use the parameter name columns as given.

df.to_csv(self, path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None)

Can above-given techniques change to columns in python?

There has been some discussion on such inconsistencies in pandas. It seems like the proposed changes are in line with what you suggested. Nonetheless, developers have not arrived at a conclusion yet. Thus, as of now, you might not use the parameter columns as an alias for names in pd.read_csv method.

If you tried passing columns as a parameter you will encounter the following error:

TypeError: parser_f() got an unexpected keyword argument 'columns'

Fortunately, you may use a decorator to achieve what you need:

import pandas as pd
from functools import wraps

def enable_columns_parameter(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        if 'columns' in kwargs:
            kwargs['names'] = kwargs.pop('columns')
        return f(*args, **kwargs)
    return wrapper

pd.read_csv = enable_columns_parameter(pd.read_csv)

After applying the decorator to the pd.read_csv function, this line would work fine without any errors:

pd.read_csv('file.csv', columns=['any', 'given', 'name'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM