Py学习  »  Python

Python基于索引添加两个数据帧(已编辑)

Alex • 5 年前 • 1903 次点击  

我用Panda和Python获得了两个数据帧。

  df1 = pd.DataFrame({'Index': ['0','1','2'], 'number':[3,'dd',1], 'people':[3,'s',3]})
df1 = df1.set_index('Index')
df2 = pd.DataFrame({'Index': ['0','1','2'], 'quantity':[3,2,'hi'], 'persons':[1,5,np.nan]})

我想根据索引对列的数量求和。列的名称不同,可能包含字符串。(事实上,每个df有50个列)。我想把nan看作0。结果应该是: df3型 索引列1列2
1南 2南

注: 当然,一段时间或一段时间都可以做到,只是不太优雅。。。

    indices=0
    columna=0
    while indices<len(df.index)-1:
         while columna<numbercolumns-1:
         df3.iloc[indices,columna]=df1.iloc[indices,columna] +df2.iloc[indices,columna] 
 indices += 1
 columna += 1

谢谢您。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53766
 
1903 次点击  
文章 [ 1 ]  |  最新文章 5 年前