0
mtu,dap
06.01.2015 00:00 - 06.01.2015 01:00,36.90

I am trying to work the comma delimited data from the picture above into pandas for further analysis with the following bit of code:

import pandas as pd

DAP = pd.read_csv('xx.csv', 
    index_col = 'mtu',
    sep = ',',
    encoding="utf-8-sig")
#DAP = DAP.set_index('mtu')
date_time = DAP['mtu']
Hourly_DAP = DAP['dap']

However it keep giving me the following error, with set_index enabled and with index_col, have tried other solutions that can be found online but none seem to solve this issue:

KeyError: 'mtu'

Would anyone be able to solve this issue?

I have updated the code according to the duplicate question to the following however now I get nameError that index is not defined. The answer to the duplicate question is very brief so cannot figure it out. The updated code is as follows, can any pick the mistake?:

import pandas as pd

DAP = pd.read_csv('xx.csv',
                  sep = ',',
                  encoding="utf-8-sig")

DAP = DAP.set_index('mtu','dap')
print(DAP.index)
index(['mtu', 'dap'], dtype='object', name='TweetID')
2
  • Likely there are spaces you are not accounting for. I suggest you copy and paste your data here in a code block, not link to an image. Commented Mar 13, 2019 at 12:52
  • Update: added an example in code of the csv file It does not allow me to copy my data in a code block, if I copy the data from the csv file it pops up as an image. Commented Mar 13, 2019 at 13:04

2 Answers 2

0

Once you make a column an index, it is not a column anymore, so you have to access it by the index. Try to make operations

date_time = DAP['mtu']
Hourly_DAP = DAP['dap']

before setting any index column, neither while reading the file

DAP = pd.read_csv('xx.csv', 
    #index_col = 'mtu',
    sep = ',',
    encoding="utf-8-sig")

or after it

#DAP = DAP.set_index('mtu','dap')
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry i do not understand what you advise me to do. Could be a bit more elaborate on the steps i have to undertake?
You have to execute the lines of date_time=... before setting the mtu column as Index. Remove the index_col argument while reading the csv and it should work.
It worked, however it gives the notification that DAP is not defined before (which is correct as it is defined afterwards). Setting the index still does not work, what is wrong when you place this after reading the file: dap.set_index=dap['datetime']
Put an example of the csv file, tell me what you want and I'll code and explain It well. However, dont forget to rate the answer If It has helped you.
0

This is what I spent my day on, will defo give you a good review if you can help out. I feel like a noob but gotto start somewhere. Thanks for your help anyways!

af = act_freq['actual_freq']
datetime = act_freq['datetime']
act_freq = pd.read_csv('xx.csv',
                  sep = ',',
                  encoding="utf-8-sig")
act_freq['datetime'] =  pd.to_datetime(act_freq['datetime'], 
        infer_datetime_format=True)

act_freq.set_index=act_freq['datetime']
grid_freq_des = 50

The following function gave me what I wanted, however I would like to do it for the whole file

print(sum(abs(grid_freq_des-(af.head(150)))))

So I spent my day setting up sth like this which i cannot get to work (

  for af in range (act_freq['actual_frequency']):
        freq_dev = grid_freq_des - af
        print(sum(freq_dev))

So summing up, I cannot set the datetime as index (python keeps giving its own index) and i would like to set up a function (freq_dev in this case) to repeat over the values of 'actual_freq' in the csv:

                  datetime  actual_freq
0      2019-01-01 00:00:00       50.038
1      2019-01-01 00:00:10       50.021
2      2019-01-01 00:00:20       50.013
3      2019-01-01 00:00:30       50.004

1 Comment

Fixed the function, but haven't been able to get the index set right

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.