Python 向数据帧添加行

Python 向数据帧添加行,python,pandas,idl,Python,Pandas,Idl,各位!!我正在构建一个代码,将.sav文件转换为.csv文件,以在IDL之外绘制列数据。我只绘制了一行,但在计算如何在数据框中绘制更多行时遇到了一些问题 我的代码: 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 =

各位!!我正在构建一个代码,将.sav文件转换为.csv文件,以在IDL之外绘制列数据。我只绘制了一行,但在计算如何在数据框中绘制更多行时遇到了一些问题

我的代码:

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)
我的输出是:

  a
0 5
1 4
2 3
3 2
4 1
5 0
我想修改代码并向数据帧添加行,以获得如下输出:

  a b
0 5 10
1 4 9
2 3 8
3 2 7
4 1 6
5 0 5

要添加新列,请执行以下操作:

dataframe["name of column"] = ["add something to column here"]
dataframe = dataframe.T
dataframe["name of row"] = ["contents of row"]
要添加新行,请执行以下操作:

dataframe["name of column"] = ["add something to column here"]
dataframe = dataframe.T
dataframe["name of row"] = ["contents of row"]
这将对其进行转置,然后添加一个新列,您可以通过再次转置使其恢复正常。
您必须使行/列的长度为

d=pd.DataFrame({'a':a,'b':b})
d['b']=d['a']+5
非常感谢!我投了反对票,因为你答案的第二部分并没有添加一行,而是在转置的数据帧中添加了一列。如果要添加行,应该使用df.append()或df.loc[i]=something