1

Apologies for this basic question. I am new to Python and having some problem with my codes. I used pandas to load in a .csv file and having problem accessing particular elements.

 import pandas as pd
 dateYTM = pd.read_csv('Date.csv')
 print(dateYTM)
 ##  Result 
 #       Date
 # 0  20030131
 # 1  20030228
 # 2  20030331
 # 3  20030430
 # 4  20030530
 #
 # Process finished with exit code 0

How can I access say the first date? I tried many difference ways but wasn't able to achieve what I want? Many thanks.

1
  • 1
    you can just do dateYTM.iloc[0] Commented Jan 27, 2016 at 9:12

1 Answer 1

1

You can use read_csv with parameter parse_dates loc, see Selection By Label:

import pandas as pd
import numpy as np
import io

temp=u"""Date,no
20030131,1
20030228,3
20030331,5
20030430,6
20030530,3
"""
#after testing replace io.StringIO(temp) to filename
dateYTM  = pd.read_csv(io.StringIO(temp), parse_dates=['Date'])
print dateYTM 
        Date  no
0 2003-01-31   1
1 2003-02-28   3
2 2003-03-31   5
3 2003-04-30   6
4 2003-05-30   3

#df.loc[index, column]

print dateYTM.loc[0, 'Date']
2003-01-31 00:00:00

print dateYTM.loc[0, 'no']
1

But if you need only one value, better is use at see Fast scalar value getting and setting:

#df.at[index, column]

print dateYTM.at[0, 'Date']
2003-01-31 00:00:00

print dateYTM.at[0, 'no']
1
Sign up to request clarification or add additional context in comments.

1 Comment

what if they are just some numeric? Maybe I still haven't had right concept in python yet. I don't quite know how to access the elements. I tried DateYTM[0], DateYTM[[0]], DateYTM[0,0] etc... and they all don't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.