0

I want to export a list of lists to a csv file. However, the csv file is not showing any of the data.

Initially I have 30 rows of 'I' data and one row of 'R' data.

I run my algorithm to obtain peak areas when R is plotted against I{i}. This gives me 30 sets of data each with three columns as follows:

>      0          1             2
>     0   3053.6   105000.0  -5217775.735
>     1   3015.9    81892.0  -4013311.400
>     2   2962.8    98694.0  -2050799.050
>     3   2936.2    67884.0  -1140645.600
>     4   2906.3  2530000.0 -22099575.600
>     5   2871.5   102000.0   -653778.650
>     6   2777.9     8482.4    -68580.440
>     7   2719.3    11768.0    -91285.610
>     8   2625.0     5902.5    -40623.500
>     9   2599.3     5304.7    -69163.680
>     10  2573.5    18009.0   -170745.690
>     11  1538.0    12694.0   -965128.025
>     12  1467.7     9279.2   -144139.995
>     13  1451.4    21626.0   -280386.495
>     14  1329.8     7739.9    -63603.430
>     15  1173.9     8096.7    -66836.410
>     16   966.3    10964.0   -101197.010
>     17   799.2    32662.0   -305534.340
>     18   164.5   124000.0  -1579972.665

Find_peaks is my function as described above.

result =[]

for i in range(1,31):
    result.append(pd.DataFrame((find_peaks(df1['R'], df1['I {}'.format(i)])), index = None))

for i in range(1,31):
    df2 = pd.DataFrame(result[i])
    df2.to_csv('Output.csv')

I'm not sure what I am doing wrong.

3
  • Can you show your debugging efforts, firstly it's a bit strange to construct a list of dfs and then construct a df again from the list content when it's already a df. It would make more sense to construct a df of all the results and write to csv once Commented Oct 27, 2015 at 14:46
  • Well, for one thing I think you're going to be rewriting your csv each time you iterate through that second for-loop. Do you have access to a debugger? If so, set a breaking point for the first and second lines of your second for-loop and see if your data is as expected. Commented Oct 27, 2015 at 14:48
  • I've somewhat rearranged my code. I am now producing a dataframe with 31 lists. However, the structure of the csv is not right. i.e. in one cell we have '(x,y,z)'. Is there a way to separate these? I am using: result.append((find_peaks(df1['R'], df1['I {}'.format(i)]))) .... and then df2 = pd.DataFrame(result) .. and finally exporting to csv... via.... df2.to_csv('Output.csv') Commented Oct 27, 2015 at 15:07

1 Answer 1

1

You can try concat this output dataframes to one:

import pandas as pd

result = []

for i in range(1,31):
    result.append(pd.DataFrame((find_peaks(df1['R'], df1['I {}'.format(i)])), index = None))
    df2 = pd.concat(result)

print df2.head()
df2.to_csv('Output.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.