1

I have two numpy array, each shaped (x,1). I wanted to export the data to CSV file so it looks something like this

Channel- Freq(np Array 1) - FFT(np Array 2) - Class (a string label)

Channel and class column will have 1 fixed value, freq and FFT comes from the array.

I have tried using np.column_stack but that only allows combining freq and fft, nothing else. I have tried looking at pandas DataFrame (which the original data comes from after importing from CSV) I can't find anything useful at the moment.

def combine_data(freq, matrix):
    combine_dt = np.column_stack((freq, matrix))
    return combine_dt

def export_data_csv(freq, matrix):
    combined_data = np.column_stack((freq, matrix))
    np.savetxt("freq_amp.csv", combined_data, delimiter=",")

This only works to make a CSV with only two columns

3
  • Have you tried numpy.hstack? This allows you to stack two columns together horizontally. You then then write to a csv normally. Commented Jun 19, 2019 at 14:33
  • Given small examples of your arrays, and the desired output. How picky are you about the format and spacing of the numeric columns? column_stack can join 3 arrays. Commented Jun 19, 2019 at 15:08
  • so the data I need to export will have more columns, It will have e.g. Channel - Frequency - FFT.real() - FFT.imag() - Class, a channel is just 1-4 (one of the channel numbers), class is just what the input signal was to generate the original data, e.g. a frequency of 50Hz. Commented Jun 19, 2019 at 16:30

1 Answer 1

1

I think it's easier using pandas.

Let's say:

array1=[0,1,2,3,4]
array2=[10,11,12,13,14]

First create a dataframe with the 2 arrays and then set the constant values:

my_df = pd.DataFrame(data={"freq":array1,"FFT":array2})
my_df["channel"]="channel_name"
my_df["class"]="class_name"

#Output
    freq    FFT     channel     class
0     0     10   channel_name   class_name
1     1     11   channel_name   class_name
2     2     12   channel_name   class_name
3     3     13   channel_name   class_name
4     4     14   channel_name   class_name

then you can easily export it to csv:

my_df.to_csv("filename.csv")

if you don't want/need the index just set:

my_df.to_csv("filename.csv",index=False)

For other options just look at the documentation pandas.to_csv()

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.