2

I'm new to python and since I'm working with tabular data I was recommended to use pandas to process and structure the data that I'll be working with.

I'm reading this file:

Accel(m/s^2) 0.2518 0.0072 9.8201


Accel(m/s^2) 0.2369 0.0036 9.8201


Accel(m/s^2) 0.2477 0.0048 9.8369



Using the following code:

file_name = '/Users/MT/Desktop/Accel_Data.txt';

accel_data = {
'X': [], 'Y': [], 'Z': []
 }




data = pd.read_csv(file_name,sep = '\t');




for line in data:
accel_data['X'].append(line[1])
accel_data['Y'].append(line[2])
accel_data['Z'].append(line[3])

I expected the accel_data dictionary to look like the following:

{
'X': [0.2518,0.2369,0.2477], 'Y': [0.0072, 0.0036, 0.0048], 'Z': [9.8201, 9.8201, 9.8369]
 }




But it looked like this instead:

{
'X': ['c', ':', ':', ':'], 'Y': [ 'c',  ' ',  ' ', ' '], 'Z': [ 'e',  '0',  '0', '9']
 }




This work could be done in excel or matlab, which I'm more accustomed to using, but I need to learn how to use python to transition into a more versatile coding language for processing this type of data. Any help would be thoroughly appreciated!

MTurra
  • 23
  • 3
  • can you try running `data.head()` to see what the dataframe looks like? Or `print(data)` – Smurphy0000 Aug 10 '20 at 14:14
  • 1
    `sep = '\t'` may be the issue. Is your file tab delimited? – thorntonc Aug 10 '20 at 14:14
  • @Smurphy0000 the output looks like this [link] (https://drive.google.com/file/d/1NrmDQ9bxxmctU9UUqcx4jZt5TMhFPpBe/view?usp=sharing). Also, pandas read_csv function defaults to a delimiter = none. So I don't believe that the file tab is delimited – MTurra Aug 10 '20 at 14:34
  • You might need to use `data.iterrows()` - see here for more details https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas – PApostol Aug 10 '20 at 14:36

1 Answers1

1

your issue seems to be the use of sep = '\t' and your file seems to be space delimited. change your line that reads to this

data = pd.read_csv(file_name, sep = ' ') # note tha space

and if your file doesn't have a header then do

data = pd.read_csv(file_name, sep = ' ', header=None)

Now let's go a bit extra.
There doesn't seem to be a reason to do what you are doing for putting the data into that dictionary, you can assign the header for the dataframe like this

columns = ['formula', 'X', 'Y', 'Z']
data = pd.read_csv(file_name, sep = ' ', header=None, names=columns)

which would give you

        formula       X       Y       Z
0  Accel(m/s^2)  0.2518  0.0072  9.8201


1  Accel(m/s^2)  0.2369  0.0036  9.8201


2  Accel(m/s^2)  0.2477  0.0048  9.8369

The power of pandas comes with using it as a dataframe, if you are to extract the data and convert it into a dictionary then you lost that.

Jimmar
  • 4,194
  • 2
  • 28
  • 43
  • That was exactly what I needed! I couldn't upvote the answer because I'm new but that did exactly what I needed and I'll be sure to store it in a dataframe going forward to take advantage of pandas. Thanks again! – MTurra Aug 10 '20 at 20:56
  • @MTurra well, if this answer does answer your question then you can mark it as accepted by clicking on the check mark under the vote buttons, you are welcome :D – Jimmar Aug 10 '20 at 21:05