-1

everyone! I'm building a code to turn a .sav file into a .csv file to plot column data outside of IDL. I've plotted a single row, but I've had some trouble figuring out how to plot further rows within the DataFrame.

My code:

import pandas as pd
import numpy as np
import scipy.io as spio
import csv
onfile = file
finalfile = file2
s = spio.readsav(onfile, python_dict=True, verbose=True)
a = np.asarray(s["a"])
b = np.asarray(s["b"])

d=dp.DataFrame(data=a,columns=['a']
d.to_csv(finalfile,sep='',encoding='utf-8',header=True)

My output is:

  a
0 5
1 4
2 3
3 2
4 1
5 0

I want to modify my code and add rows to the DataFrame to get an output like so:

  a b
0 5 10
1 4 9
2 3 8
3 2 7
4 1 6
5 0 5
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
haramassive
  • 163
  • 1
  • 8

1 Answers1

-1

to add a new column you would do something like:

dataframe["name of column"] = ["add something to column here"]

to add a new row you would do something like:

dataframe = dataframe.T
dataframe["name of row"] = ["contents of row"]

this transposes it then adds a new column, you can put it back to normal by transposing it again. you have to make the length as long as the row/column

haley chen
  • 26
  • 4
  • 1
    I downvoted becuase the second part of your answer does not add a row but adds a column to a transposed dataframe. If you want to add a row you should use df.append() or df.loc[i] = something – Dodge Aug 21 '18 at 23:03