0

I am beginner in Python and I am stuck with data which is array of 32763 number, separated by comma. Please find the data here data

I want to convert this into two column 1 from (0:16382) and 2nd column from (2:32763). in the end I want to plot column 1 as x axis and column 2 as Y axis. I tried the following code but I am not able to extract the columns

import numpy as np
import pandas as pd
import matplotlib as plt

data = np.genfromtxt('oscilloscope.txt',delimiter=',')
df = pd.DataFrame(data.flatten())
print(df)

and then I want to write the data in some file let us say data1 in the format as shown in attached pic data for

1
  • I can't write an answer right now but you could just split on your input string, then create the data frame with two columns using the array. You can use range (0:16382) on the array in order to define want is the content of each column. Commented Nov 14, 2020 at 10:59

2 Answers 2

1

It is hard to answer without seeing the format of your data, but you can try

data = np.genfromtxt('oscilloscope.txt',delimiter=',')
print(data.shape) # here we check we got something useful

# this should split data into x,y at position 16381
x = data[:16381]
y = data[16381:]


# now you can create a dataframe and print to file
df = pd.DataFrame({'x':x, 'y':y})
df.to_csv('data1.csv', index=False)
Sign up to request clarification or add additional context in comments.

5 Comments

so it should work except 32763 is an odd number and you need x,y to be of the same length. how do you want to deal with that?
It was mistake from my side. I made it to read the x upto 16381 than this works. I wanted to write this into file again, so I wrote something like this (which is not writnf anything) time = str(x) ch1data = str(y) file = open("oscilloscope1.txt","w") file.write(time+ "\t" + ch1data) file.close
looks ok-ish except you probably want file.close() (note the parenthesis). This should write to a file but may not be in the format you want. how do you want it in the file? please provide a snippet of you desired output
Thanks for the help, I have modified my question for the format
and I modified my answer
0

Try this.

#input as dataframe df, its chunk_size, extract output as list. you can mention chunksize what you want.

def split_dataframe(df, chunk_size = 16382): 
    chunks = list()
    num_chunks = len(df) // chunk_size + 1
    for i in range(num_chunks):
    chunks.append(df[i*chunk_size:(i+1)*chunk_size])
return chunks

or

np.array_split

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.