简体   繁体   English

通过将行熊猫中的NaN值与列匹配来将数据帧中的一行添加到另一行中

[英]Adding a row from a dataframe into another by matching columns with NaN values in row pandas python

The Scenario: 场景:

I have 2 dataframes fc0 and yc0 . 我有2个数据帧fc0yc0 Where fc0 is a Cluster and yc0 is another dataframe which needs to be merged in fc0 . 其中, fc0是一个Cluster,而yc0是另一个数据帧,需要将其合并到fc0

The Nature of data is as follows: 数据的性质如下:

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 yc0

iid  uid    1    2    5    6    9    15
0    944  5.0  3.0  4.0  3.0  3.0  5.0 

The Twist 扭曲

I have 1682 columns in fc0 and I have few hundered values in yc0 . 我在fc0中有1682列,而在yc0中我的值很少 Now I need the yc0 to go into fc0 现在我需要yc0进入fc0

In haste of resolving it, I even tried yc0.reset_index(inplace=True) but wasn't really helpful. 为了解决这个问题,我什至尝试了yc0.reset_index(inplace=True)但并没有真正的帮助。

Expected Output 预期产量

     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

References 参考

Link1 Tried this, but landed up inserting NaN values for 1st 16 Columns and rest of the data shifted by that many columns Link1对此进行了尝试,但是在第1 16列插入了NaN值,其余数据移动了那么多列

Link2 Couldn't match column keys, besides I tried it for row. Link2无法匹配列键,除了我尝试了行键。

Link3 Merging doesn't match the columns in it. Link3合并不匹配其中的列。

Link4 Concatenation doesn't work that way. Link4串联无法正常工作。

Link5 Same issues with Join. Link5与Join相同的问题。

EDIT 1 编辑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

and

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

Here's a MVCE example. 这是MVCE示例。 Does this small sample data show the functionality that you are expecting? 这个小的样本数据是否显示您期望的功能?

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)

Output: 输出:

      A   B   C     D   E
0  81.0  57  54   NaN  88
1  63.0  63  74   NaN  10
2  13.0  89  88   NaN  66
3  90.0  81   3   NaN  31
4  66.0  93  55   NaN   4
0   NaN  93  48  62.0  25
1   NaN  24  97  52.0  88
2   NaN  53  50  21.0  13
3   NaN  81  27   7.0  81
4   NaN  10  21  77.0  19

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从系列/ dict中的匹配列更新pandas数据帧行值 - Update pandas dataframe row values from matching columns in a series/dict 根据 Python 中另一个 dataframe 的行值从 dataframe 中获取列? - Taking columns from a dataframe based on row values of another dataframe in Python? Pandas 基于另一个 dataframe 将多个列和行值设置为 nan - Pandas Set multiple column and row values to nan based on another dataframe 在 Pandas Python 中添加一行并将值附加到列 - Adding a row and appending values to columns in Pandas Python Python - 消除 numpy 数组或 pandas Z6A8064B53DF4794555570C53DF4794555570 - Python - Eliminating NaN values in each row of a numpy array or pandas dataframe 通过匹配 Pandas DataFrame 中另一列中的值来区分行值 - Difference of row values by matching values in another column in a Pandas DataFrame python pandas数据框乘以匹配索引或行名的列 - python pandas dataframe multiply columns matching index or row name 根据某些列向 pandas dataframe 添加标题/另一行 - Adding a header/another row to a pandas dataframe based on some columns 从Pandas数据框中的某一行获取某些列值,然后将其添加到另一数据框中 - Taking certain column values from one row in a Pandas Dataframe and adding them into another dataframe Python pandas:将多个列替换为与另一个数据帧中的多个列匹配的值 - Python pandas: replace values multiple columns matching multiple columns from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM