3

I am saving a csv file by reading my serial port. Few lines of my csv file:

*
20/01/20,10:13:59,"11   22
"
20/01/20,10:13:59,"11   22
"
20/01/20,10:13:59,"11   22
"
*

I would like to get the date, time and my 2 data all in a list as a[0] as date, a[1] as time, a[1]=data1, a[3]=data2

But I am not able to separate the 2 data, they come in the same column this is the code snippet

from pandas import read_csv

df = read_csv('test_csvtry1.csv')

output is:

A dataframe 7x3 but i want it as 7x4

"

1   20/01/20    10:13:59    "11 22
"

2   20/01/20    10:13:59    "11 22
"

3   20/01/20    10:13:59    "11 22
"

4   20/01/20    10:13:59    "11 22
"

5   20/01/20    10:13:59    "11 22
"

6   20/01/20    10:13:59    "11 22
"

Kindly can someone let me know how this can be done?

2
  • Could you please share some of the code you're using as well as the erroneous output you're receiving? Commented Jan 20, 2020 at 5:11
  • What is data1 and data 2? If they correspond to 11 and 12 in your data, they belong to the same cell in the csv which is why you the dataframe reads it as the same cell value. Are you generating this csv in previous code too? Commented Jan 20, 2020 at 5:21

4 Answers 4

2

Why to use panda for something so basic?

import csv

with open("date_time_data.csv", 'r') as f:
    f_csv = csv.reader(f)
    for line in f_csv:
        a0, a1, a2 = line
        print(a0,a1,a2)

output

20/01/20 10:13:59 11   22

20/01/20 10:13:59 11   22

20/01/20 10:13:59 11   22
Sign up to request clarification or add additional context in comments.

Comments

0

To split the data1 and data2 in the read_csv function itself, here's what you could do.

from pandas import read_csv

df = read_csv('test_csvtry1.csv', header=None, delim_whitespace=True)

This would give you the 7x4 dataframe, you're looking for.

Comments

0

You need to split the data part using the delimiter between data1 and data2.

In this case the delimiter is space.

Get you concatenated data into some variable and do below split operation.

splitdata = data.split(" ")
data1, data2 = splitdata[0], splitdata[1]

While reading the CSV you will always get it in same column. You need to split and store in different column manually.

Create an empty column into your dataframe.

df['data2'] = np.nan

Now,

  1. Iterate over your dataframe and pick the data value.
  2. Split the data value using above method and replace current data value with data1.
  3. Store data2 into the newly created column.

Hope this should solve your problem.

for row in df.itertuples():
    data = row.data
    splitdata = data.split(" ")
    data1, data2 = splitdata[0], splitdata[1]
    row.data = data1
    row.data2 = data2

Comments

0

Use multiple seperators while reading the csv file seperators being ["] , [,] ,and [ ]

df = pd.read_csv("test_csvtry1.csv" , sep='"|,| ', header = None)

df

0   0   20/01/20    10:13:59    NaN   11    22
1   1   20/01/20    10:13:59    NaN   11    22
2   2   20/01/20    10:13:59    NaN   11    22

Drop the column with Nan values.

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.