向 DataFrame 添加行
Adding rows to a DataFrame
大家好!我正在构建一个代码,将 .sav 文件转换为 .csv 文件,以在 IDL 之外绘制列数据。我绘制了一行,但我在弄清楚如何在 DataFrame 中绘制更多行时遇到了一些问题。
我的代码:
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
我想修改我的代码并将行添加到 DataFrame 以获得如下输出:
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"]
这个转置然后添加一个新的列,你可以通过再次转置它恢复正常。
你必须使长度与 row/column
一样长
大家好!我正在构建一个代码,将 .sav 文件转换为 .csv 文件,以在 IDL 之外绘制列数据。我绘制了一行,但我在弄清楚如何在 DataFrame 中绘制更多行时遇到了一些问题。
我的代码:
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
我想修改我的代码并将行添加到 DataFrame 以获得如下输出:
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"]
这个转置然后添加一个新的列,你可以通过再次转置它恢复正常。 你必须使长度与 row/column
一样长