Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在数据框中插入行(列表)?_Python_Pandas - Fatal编程技术网

Python 如何在数据框中插入行(列表)?

Python 如何在数据框中插入行(列表)?,python,pandas,Python,Pandas,伙计们 我有一个列定义的DataFrame和如下列表: df = pd.DataFrame(columns=list('ABCDEF')) [0.25, 1, [97, 99], array([18., 16., 17.]), array([ 31., 30., 29.]), array([ 0.])] 实际上,此列表中的每个元素都是我期望的DataFrame的一列,我希望有如下内容: A B C D

伙计们

我有一个列定义的DataFrame和如下列表:

df = pd.DataFrame(columns=list('ABCDEF'))

[0.25, 1, [97, 99], array([18.,  16.,  17.]), array([ 31.,  30.,  29.]), array([ 0.])]
实际上,此列表中的每个元素都是我期望的DataFrame的一列,我希望有如下内容:

A       B         C                  D                    E             F
0.25    1   [97,99]  [18.,  16.,  17.]   [ 31.,  30.,  29.]         [ 0.]
我可以在while循环中插入列表,即在数据帧中插入一行,例如,在第二个循环中,我有另一行,如:

[0.25,2[132,134],数组([17.]),数组([29,30,31.]),数组([15,16.])]

但当我创建DataFrame时,它总是将数组中的元素放在一列中:

                                0    A    B    C    D    E    F
0                            0.25  NaN  NaN  NaN  NaN  NaN  NaN
1                               1  NaN  NaN  NaN  NaN  NaN  NaN
2  [97.7123918594, 99.7123918594]  NaN  NaN  NaN  NaN  NaN  NaN
3  [17.0, 24.0, 18.0, 16.0, 17.0]  NaN  NaN  NaN  NaN  NaN  NaN
4              [31.0, 30.0, 29.0]  NaN  NaN  NaN  NaN  NaN  NaN
5                           [0.0]  NaN  NaN  NaN  NaN  NaN  NaN

有什么办法能达到我的期望吗?非常感谢。

您可以使用
.loc

In [324]: df = pd.DataFrame(columns=list('ABCDEF'))

In [325]: l1 = [0.25, 1, [97, 99], array([18.,  16.,  17.]), array([ 31.,  30.,  29.]), array([ 0.])]

In [326]: df.loc[len(df.index)] = l1

In [327]: df
Out[327]:
      A  B         C                   D                   E      F
0  0.25  1  [97, 99]  [18.0, 16.0, 17.0]  [31.0, 30.0, 29.0]  [0.0]

但是,如果您预先有列表列表,您可以

pd.DataFrame([l1, l2, ..., ln], columns=list('abcdef'))

您可以使用
.loc

In [324]: df = pd.DataFrame(columns=list('ABCDEF'))

In [325]: l1 = [0.25, 1, [97, 99], array([18.,  16.,  17.]), array([ 31.,  30.,  29.]), array([ 0.])]

In [326]: df.loc[len(df.index)] = l1

In [327]: df
Out[327]:
      A  B         C                   D                   E      F
0  0.25  1  [97, 99]  [18.0, 16.0, 17.0]  [31.0, 30.0, 29.0]  [0.0]

但是,如果您预先有列表列表,您可以

pd.DataFrame([l1, l2, ..., ln], columns=list('abcdef'))

是的,我赢了,非常好。是的,我赢了,非常好。可能的复制品可能的复制品