0

I have csv data that looks like this:

1471361094509,doorLowerPosition,-73.3348875
1471361094509,doorUpperPosition,-3.29595
1471361094512,sectionLowerCurrFiltered,-0.2
1471361094512,actuatorLowerFrontDuty,0.0
1471361094515,doorCtrlStatus,5.0
1471361094515,SMState,14.0
1471361094516,lateralAccel,25.55
1471361094516,longitudinalAccel,25.55 
1471361094519,ambientTemperature,23.5

Which I would like to import into a DataFrame with the the leftmost value being the index, the middle a column identifier and the rightmost, the value for the column at that index.

Previously I was separating these and importing them manually, then joining into a single frame. I wonder if there is quicker way to do it in one step.

1 Answer 1

3

Read the data into a DataFrame using pd.read_csv, and then pivot:

import pandas as pd
df = pd.read_csv('data', header=None, names=['index','columns','value'])
df = df.pivot('index', 'columns', 'value')
print(df)

yields

columns        SMState  actuatorLowerFrontDuty  ambientTemperature  \
index                                                                
1471361094509      NaN                     NaN                 NaN   
1471361094512      NaN                     0.0                 NaN   
1471361094515     14.0                     NaN                 NaN   
1471361094516      NaN                     NaN                 NaN   
1471361094519      NaN                     NaN                23.5   

columns        doorCtrlStatus  doorLowerPosition  doorUpperPosition  \
index                                                                 
1471361094509             NaN         -73.334887           -3.29595   
1471361094512             NaN                NaN                NaN   
1471361094515             5.0                NaN                NaN   
1471361094516             NaN                NaN                NaN   
1471361094519             NaN                NaN                NaN   

columns        lateralAccel  longitudinalAccel  sectionLowerCurrFiltered  
index                                                                     
1471361094509           NaN                NaN                       NaN  
1471361094512           NaN                NaN                      -0.2  
1471361094515           NaN                NaN                       NaN  
1471361094516         25.55              25.55                       NaN  
1471361094519           NaN                NaN                       NaN  
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.