Home > Net >  Python: Get only one column of pandas dataframe (without index) and put it into deque
Python: Get only one column of pandas dataframe (without index) and put it into deque

Time:11-19

I have a .csv of different companies of form

Date (Key) Company 1 Company 2 ... Company n
01.01.2020 2 11 ... 3
02.01.2020 3 9 ... 45
... ... ... ... ...
01.11.2021 1 12 ... 34

the companies themself I saved in a ticker file. My aim now is to load this stuff of data in a deque of following form

[[Company 1] [2 3 ... 1] [Company 2] [11 0 ... 12] ... [Company n] [3 45 ... 34]]

with the code

import pandas as pd
import pickle

from collections import deque

with open("Webscrapper/Sp500tickers.pickle", 'rb') as f:
    tickers = pickle.load(f)

df = pd.read_csv(f"Webscrapper/Sp500tickers_DailyChanges.csv", index_col="Date")

data = [[ticker, df[ticker][0:-1]] for ticker in tickers]
c = deque(data)
print(c)

I get the Dates too, means an array of form

[[Company 1] [Date Company 1 01.01.2020 2 02.01.2020 2 3 ... 01.11.2021 1] [Company 2] [Date Company 2 01.01.2020 11 02.01.2020 0 ... 01.11.2021 12] ... [Company n] [Date Company n 01.01.2020 3 02.01.2020 45 ... 01.11.2021 34]]

How can I convert the array in the desired form?

CodePudding user response:

Use Series.tolist to convert the columns' values to lists

data = [[ticker, df[ticker].tolist()] for ticker in tickers]
c = deque(data)
  • Related