通过将列与行pandas python中的NaN值匹配,将数据帧中的行添加到另一个数据帧中

2024-10-03 15:26:49 发布

您现在位置:Python中文网/ 问答频道 /正文

场景:

我有两个数据帧fc0yc0。其中fc0是一个集群,yc0是另一个需要合并到fc0中的数据帧。在

数据性质如下:

fc0

uid         1         2         3         4         5         6  
234  235  4.000000  4.074464  4.128026  3.973045  3.921663  4.024864   
235  236  3.524208  3.125669  3.652112  3.626923  3.524318  3.650589   
236  237  4.174080  4.226267  4.200133  4.150983  4.124157  4.200052

yc0

^{pr2}$

扭转

我在fc0中有1682列,而在yc0中我几乎没有百分之一百的值。现在我需要yc0进入fc0

为了尽快解决这个问题,我甚至尝试了yc0.reset_index(inplace=True),但没有真正的帮助。在

预期产量

     uid         1         2         3         4         5         6  
234  235  4.000000  4.074464  4.128026  3.973045  3.921663  4.024864   
235  236  3.524208  3.125669  3.652112  3.626923  3.524318  3.650589   
236  237  4.174080  4.226267  4.200133  4.150983  4.124157  4.200052
944  5.0       3.0       NaN       NaN       4.0       3.0       3.0

参考文献

Link1尝试了这种方法,但结果插入了前16列的NaN值,其余的数据则被移动了那么多列

Link2无法匹配列键,此外,我尝试将其用于行。在

Link3合并与其中的列不匹配。在

Link4连接不起作用。在

Link5Join的问题相同。在

编辑1

fc0.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 235 entries, 234 to 468
Columns: 1683 entries, uid to 1682
dtypes: float64(1682), int64(1)
memory usage: 3.0 MB

以及

yc0.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1 entries, 0 to 0
Columns: 336 entries, uid to 1007
dtypes: float64(335), int64(1)
memory usage: 2.7 KB

Tags: columnsto数据coreinfodataframepandasuid
1条回答
网友
1楼 · 发布于 2024-10-03 15:26:49

下面是一个MVCE示例。这个小样本数据是否显示了您期望的功能?在

df1 = pd.DataFrame(np.random.randint(0,100,(5,4)), columns=list('ABCE'))

    A   B   C   E
0  81  57  54  88
1  63  63  74  10
2  13  89  88  66
3  90  81   3  31
4  66  93  55   4

df2 = pd.DataFrame(np.random.randint(0,100,(5,4)), columns=list('BCDE'))

    B   C   D   E
0  93  48  62  25
1  24  97  52  88
2  53  50  21  13
3  81  27   7  81
4  10  21  77  19

df_out = pd.concat([df1,df2])
print(df_out)

输出:

^{pr2}$

相关问题 更多 >