连接熊猫数据帧,获取第一个数据帧的Nan值

问题描述 投票:0回答:1

我正在尝试合并两个数据框。 “ df”是我的初始数据帧,其中包含我需要的所有标头信息。 “行”是我要附加到“ df”的第一行数据。

df =
   FName E1         E2          E3          E4          E5          E6
0  Nan   2          2           2           2           2           2
1  Nan   1          1           1           1           1           1
2  Nan   3          4           5           6           7           8
3  Nan   4          5           6           7           8           10
4  Nan   1002003004 1002004005  1002005006  1002006007  1002007008  1002008010


row =
   0                                        1       2       3       4       5       6
0  501#_ZMB_2019-04-03_070528_reciprocals   30.0193 30.0193 30.0193 34.8858 34.8858 34.8858

我正在尝试创建这个:

   FName                                    E1          E2          E3          E4          E5          E6
0  Nan                                      2           2           2           2           2           2
1  Nan                                      1           1           1           1           1           1
2  Nan                                      3           4           5           6           7           8
3  Nan                                      4           5           6           7           8           10
4  Nan                                      1002003004  1002004005  1002005006  1002006007  1002007008  1002008010
5  501#_ZMB_2019-04-03_070528_reciprocals   30.0193     30.0193     30.0193     34.8858     34.8858     34.8858

我尝试了以下操作:

df = df.append(row, ignore_index=True)

df = pd.concat([df, row], ignore_index=True)

这两者都会导致第一个df中的所有数据丢失,其中应包含所有标头信息。

   0                                        1       2       3       4       5       6
0  Nan                                      Nan     Nan     Nan     Nan     Nan     Nan
1  Nan                                      Nan     Nan     Nan     Nan     Nan     Nan
2  Nan                                      Nan     Nan     Nan     Nan     Nan     Nan
3  Nan                                      Nan     Nan     Nan     Nan     Nan     Nan
4  Nan                                      Nan     Nan     Nan     Nan     Nan     Nan
5  501#_ZMB_2019-04-03_070528_reciprocals   30.0193 30.0193 30.0193 34.8858 34.8858 34.8858

我也尝试过

df = pd.concat([df.reset_index(drop=True, inplace=True), row.reset_index(drop=True, inplace=True)])

哪个产生了以下回溯

Traceback (most recent call last):

  File "<ipython-input-146-3c1ecbd1987c>", line 1, in <module>
    df = pd.concat([df.reset_index(drop=True, inplace=True), row.reset_index(drop=True, inplace=True)])

  File "C:\Users\russells\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py", line 228, in concat
    copy=copy, sort=sort)

  File "C:\Users\russells\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\reshape\concat.py", line 280, in __init__
    raise ValueError('All objects passed were None')

ValueError: All objects passed were None

有人知道我在做什么错吗?

python-3.x pandas dataframe concat
1个回答
1
投票

当连接额外的行时,pandas对齐当前不重叠的列。 rename将完成工作:

pd.concat([df, row.rename(columns=dict(zip(row.columns, df.columns)))],
           ignore_index=True)

                                    FName          E1          E2          E3          E4          E5          E6
0                                     Nan           2           2           2           2           2           2
1                                     Nan           1           1           1           1           1           1
2                                     Nan           3           4           5           6           7           8
3                                     Nan           4           5           6           7           8          10
4                                     Nan  1002003004  1002004005  1002005006  1002006007  1002007008  1002008010
5  501#_ZMB_2019-04-03_070528_reciprocals     30.0193     30.0193     30.0193     34.8858     34.8858     34.8858

或者,如果您只需要在末尾分配一行,并且在RangeIndex上有一个df

df.loc[df.shape[0], :] = row.to_numpy()
© www.soinside.com 2019 - 2024. All rights reserved.