Python: How can I read csv and clean my data in a loop

spidermarn

I have the following code below which reads in csv and cleans them for 5 different financial assets. The code seems repetitive and inefficient. Is there a way I can do this in a loop instead of having to declare the 5 assets variables one-by-one?

import pandas as pd

# Input symbols
stock_symbol = "VTI"
ltb_symbol = "TLT"
stb_symbol = "IEI"
gld_symbol = "GLD"
comms_symbol = "DBC"

session_type = "open" # open, high, low or close

# Read csv for all symbols in Dataframes
stock_df = pd.read_csv("BATS_{}, 1D.csv".format(stock_symbol))
ltb_df = pd.read_csv("BATS_{}, 1D.csv".format(ltb_symbol))
stb_df = pd.read_csv("BATS_{}, 1D.csv".format(stb_symbol))
gld_df = pd.read_csv("BATS_{}, 1D.csv".format(gld_symbol))
comms_df = pd.read_csv("BATS_{}, 1D.csv".format(comms_symbol))

#Clean Dataframes
stock_df = stock_df.rename({'{}'.format(session_type): '{}'.format(stock_symbol)}, axis=1)\
            .loc[:,['time','{}'.format(stock_symbol)]]
ltb_df = ltb_df.rename({'{}'.format(session_type): '{}'.format(ltb_symbol)}, axis=1)\
            .loc[:,['time','{}'.format(ltb_symbol)]]
stb_df = stb_df.rename({'{}'.format(session_type): '{}'.format(stb_symbol)}, axis=1)\
            .loc[:,['time','{}'.format(stb_symbol)]]
gld_df = gld_df.rename({'{}'.format(session_type): '{}'.format(gld_symbol)}, axis=1)\
            .loc[:,['time','{}'.format(gld_symbol)]]
comms_df = comms_df.rename({'{}'.format(session_type): '{}'.format(comms_symbol)}, axis=1)\
            .loc[:,['time','{}'.format(comms_symbol)]]
jezrael

Create dictionary of DataFrames:

symbols = [stock_symbol,ltb_symbol,stb_symbol,gld_symbol,comms_symbol]
session_type = "open" # open, high, low or close

# Read csv for all symbols in Dataframes

dfs = {s: pd.read_csv("BATS_{}, 1D.csv".format(s)).rename({'{}'.format(session_type): '{}'.format(stock_symbol)}, axis=1).loc[:,['time','{}'.format(stock_symbol)]] for s in symbols}

You can also filter only 2 columns in read_csv by usecols parameter:

dfs = {s: pd.read_csv("BATS_{}, 1D.csv".format(s), usecols=['time',session_type]).rename({session_type: stock_symbol}, axis=1) for s in symbols}

And last is possible for DataFrames select by keys:

print (dfs['VTI'])

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

When using CI, how can I keep my environments clean?

분류에서Dev

How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

분류에서Dev

How can I read a big CSV file without Memory exhausted?

분류에서Dev

How can I subset a data frame based on dates, when my dates column is not the index in Python?

분류에서Dev

Python string clean up: How can I remove the excess of newlines of a string in python?

분류에서Dev

Python string clean up: How can I remove the excess of newlines of a string in python?

분류에서Dev

How can i transpose my json data horizontally using ruby

분류에서Dev

How can I sync my data from an external database in Rails?

분류에서Dev

How can I create Unity dash integration for my Python script?

분류에서Dev

How can I read a CSV from Webserver using c# with LINQ? (ClickOnce deployment)

분류에서Dev

How can I get clean Gradle output on Travis CI?

분류에서Dev

How can I create a clean shell environment for temporary use?

분류에서Dev

Why does my Linux version name read Linaro instead of Xubuntu and how can I fix it?

분류에서Dev

How can I read a CLongBinary field from my database and write it into a CFile object?

분류에서Dev

How read/write data on my laptop BIOS?

분류에서Dev

How i can get the td data getting from css to attach to my current row ,and use for my logic

분류에서Dev

how can i link an XML data file from my hard drive (not web) to my project?

분류에서Dev

how can i get my local DB data to fill my txt box using LINQ?

분류에서Dev

In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

분류에서Dev

How can I print a complex dictionary data type in Python?

분류에서Dev

How can I read data from CT10 bluetooth barcode scanner in Phonegap?

분류에서Dev

How can I use python pandas to parse CSV into the format I want?

분류에서Dev

Can I get data from my MSSql server and upload it to app engine via some python scripts?

분류에서Dev

How can I add a new object to an existing Entity, PUT the data and have EF add it to my database?

분류에서Dev

How can I php validate my form data so that it has to include content?

분류에서Dev

How can I update text-label in d3 donut according with my array's data

분류에서Dev

I have json data and i want to select some items only how can i do it in python?

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

How can I iterate through a function with for loop?

Related 관련 기사

  1. 1

    When using CI, how can I keep my environments clean?

  2. 2

    How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

  3. 3

    How can I read a big CSV file without Memory exhausted?

  4. 4

    How can I subset a data frame based on dates, when my dates column is not the index in Python?

  5. 5

    Python string clean up: How can I remove the excess of newlines of a string in python?

  6. 6

    Python string clean up: How can I remove the excess of newlines of a string in python?

  7. 7

    How can i transpose my json data horizontally using ruby

  8. 8

    How can I sync my data from an external database in Rails?

  9. 9

    How can I create Unity dash integration for my Python script?

  10. 10

    How can I read a CSV from Webserver using c# with LINQ? (ClickOnce deployment)

  11. 11

    How can I get clean Gradle output on Travis CI?

  12. 12

    How can I create a clean shell environment for temporary use?

  13. 13

    Why does my Linux version name read Linaro instead of Xubuntu and how can I fix it?

  14. 14

    How can I read a CLongBinary field from my database and write it into a CFile object?

  15. 15

    How read/write data on my laptop BIOS?

  16. 16

    How i can get the td data getting from css to attach to my current row ,and use for my logic

  17. 17

    how can i link an XML data file from my hard drive (not web) to my project?

  18. 18

    how can i get my local DB data to fill my txt box using LINQ?

  19. 19

    In Python, how to write strings in a file in a loop and before the loop stops, I can get the results runtimely?

  20. 20

    How can I print a complex dictionary data type in Python?

  21. 21

    How can I read data from CT10 bluetooth barcode scanner in Phonegap?

  22. 22

    How can I use python pandas to parse CSV into the format I want?

  23. 23

    Can I get data from my MSSql server and upload it to app engine via some python scripts?

  24. 24

    How can I add a new object to an existing Entity, PUT the data and have EF add it to my database?

  25. 25

    How can I php validate my form data so that it has to include content?

  26. 26

    How can I update text-label in d3 donut according with my array's data

  27. 27

    I have json data and i want to select some items only how can i do it in python?

  28. 28

    How can I break a symbolic link loop?

  29. 29

    How can I iterate through a function with for loop?

뜨겁다태그

보관