J
J
Johnem2020-05-09 19:04:04
Python
Johnem, 2020-05-09 19:04:04

How to tell pandas the correct sequence of elements in a list?

There is such a code (sorry for the length, I know that it is not according to pep8):

data_designation = ["Дата заключения", "Валюта", "Код финансового инструмента", "Операция", "Количество", "Цена", "Объём сделки"]
securities_transactions = pandas.read_excel(file_location, usecols = data_designation, dtype = {data_designation[0]: str, data_designation[1]: str, data_designation[2]: str, data_designation[3]: str, data_designation[4]: float, data_designation[5]: float, data_designation[6]: float}).values.tolist()

Elements in the list of securities_transactions , after the completion of the code, are located like this: "Date of conclusion", "Code of financial instrument", "Operation", "Quantity", "Price", "Volume of transaction" . And it is necessary that they be located as in the data_designation list .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-05-09
@Johnem

usecols: int, str, list-like, or callable default None

        If None, then parse all columns.

        If str, then indicates comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are inclusive of both sides.
        If list of int, then indicates list of column numbers to be parsed.
        If list of string, then indicates list of column names to be parsed.
        New in version 0.24.0.
        If callable, then evaluate each column name against it and parse the column if the callable returns True.
    Returns a subset of the columns according to behavior above.

Those. using this option, you specify which columns should be read from the file, while their order will remain the same as in the file, therefore, after reading the file, you need to change the order of the columns:
data_designation = ["Дата заключения", "Валюта", "Код финансового инструмента", "Операция", "Количество", "Цена", "Объём сделки"]
securities_transactions = pandas.read_excel(file_location, usecols = data_designation)
securities_transactions = securities_transactions[data_designation]
securities_transactions = securities_transactions.values.tolist()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question