1

I have a txt file "TempData.txt" which has the following format:

CODE    O/F Valid Date  MAX MIN AVG
K3T5    O   1995/01/01  51  36  44
K3T5    O   1995/01/02  45  33  39
K3T5    O   1995/01/03  48  38  43

I am trying to create a dictionary with 'ValidDates', 'Max' and 'Min' elements in it.

I am trying the following:

import pandas as pd
df = pd.read_csv(r'C:\TempData.txt', sep = "\t", header = 0)

df.columns.tolist() #prints: 'CODE', 'O/F', 'Valid Date', 'MAX', 'MIN', 'AVG'
Max = df([4])

I get the error when I try to separate the Max colum:

TypeError: 'DataFrame' object is not callable
2
  • 2
    I think you can use max_col = df['MAX'] Commented May 19, 2016 at 15:26
  • TypeError: 'DataFrame' object is not callable that means the DataFrame class has not re-implement __call__ function. You can't call a object which has not re-implement __call__ function. Type df['MAX'], you can get that column. Commented May 19, 2016 at 15:43

1 Answer 1

2

I think you can use:

max_col = df['MAX']

print (max_col)
0    51
1    45
2    48
Name: MAX, dtype: int64

If you want select 4. column use iloc:

max_col = df.iloc[:, 3] #3, because python counts 0,1,2,3

print (max_col)
0    51
1    45
2    48
Name: MAX, dtype: int64

First you can omit header=0, because it is default value in read_csv and add parse_dates for converting Valid Date to datetime.

If need dict from columns Valid Date,MAX,MIN use to_dict, if you want different format of dict, try add parameter orient:

df = pd.read_csv(r'C:\TempData.txt', sep = "\t", parse_dates=[2])
print (df)
   CODE O/F Valid Date  MAX  MIN  AVG
0  K3T5   O 1995-01-01   51   36   44
1  K3T5   O 1995-01-02   45   33   39
2  K3T5   O 1995-01-03   48   38   43


print (df[['Valid Date','MAX','MIN']])
  Valid Date  MAX  MIN
0 1995-01-01   51   36
1 1995-01-02   45   33
2 1995-01-03   48   38

print (df[['Valid Date','MAX','MIN']].to_dict())
{'MAX': {0: 51, 1: 45, 2: 48}, 
'MIN': {0: 36, 1: 33, 2: 38}, 
'Valid Date': {0: Timestamp('1995-01-01 00:00:00'), 1: Timestamp('1995-01-02 00:00:00'), 2: Timestamp('1995-01-03 00:00:00')}}

print (df[['Valid Date','MAX','MIN']].to_dict(orient='split'))
{'data': [['1995/01/01', 51, 36], ['1995/01/02', 45, 33], ['1995/01/03', 48, 38]], 'index': [0, 1, 2], 'columns': ['Valid Date', 'MAX', 'MIN']}
Sign up to request clarification or add additional context in comments.

Comments

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.