pandas reading csv orientation

前端 未结 2 633
南笙
南笙 2021-02-15 04:53

Hei I\'m trying to read in pandas the csv file you can download from here (euribor rates I think you can imagine the reason I would like to have this file!). The file is a CSV f

相关标签:
2条回答
  • 2021-02-15 05:05

    A pandas dataframe has a .transpose() method, but it doesn't like all the empty rows in this file. Here's how to get it cleaned up:

    df = pandas.read_csv("hist_EURIBOR_2012.csv")  # Read the file
    df = df[:15]    # Chop off the empty rows beyond 12m
    df2 = df.transpose()
    df2 = df2[:88]  # Chop off what were empty columns (I guess you should increase 88 as more data is added.
    

    Of course, you can chain these together:

    df2 = pandas.read_csv("hist_EURIBOR_2012.csv")[:15].transpose()[:88]
    

    Then df2['3m'] is the data you want, but the dates are still stored as strings. I'm not quite sure how to convert it to a DateIndex.

    0 讨论(0)
  • 2021-02-15 05:09

    I've never used pandas for csv processing. I just use the standard Python lib csv functions as these use iterators.

    import csv
    myCSVfile=r"c:/Documents and Settings/Jason/Desktop/hist_EURIBOR_2012.csv"
    f=open(myCSVfile,"r")
    reader=csv.reader(f,delimiter=',')
    data=[]
    for l in reader:
        if l[0].strip()=="3m":
            data.append(l)
    
    f.close()
    
    0 讨论(0)
提交回复
热议问题