【发布时间】:2017-08-24 13:05:27
【问题描述】:
示例代码和输出:
data_country1 = {'Country': [np.NaN, 'India', 'Brazil'],
'Capital': [np.NaN, 'New Delhi', 'Brasília'],
'Population': [np.NaN, 1303171035, 207847528]}
df_country1 = pd.DataFrame(data_country1, columns=['Country', 'Capital', 'Population'])
data_country2= {'Country': ['Belgium', 'India', 'Brazil'],
'Capital': ['Brussels', 'New Delhi', 'Brasília'],
'Population': [102283932, 1303171035, 207847528]}
df_country2 = pd.DataFrame(data_country2, columns=['Country', 'Capital', 'Population'])
print(df_country1)
print(df_country2)
Country Capital Population
0 NaN NaN NaN
1 India New Delhi 1.303171e+09
2 Brazil Brasília 2.078475e+08
Country Capital Population
0 Belgium Brussels 102283932
1 India New Delhi 1303171035
2 Brazil Brasília 207847528
在第一个 DataFrame 中,对于由 ALL NaN 组成的 每一 行,我想用另一个数据帧中的一行替换整行。在此示例中,第二个数据帧中的行 0,因此第一个 df 最终与第二个数据帧具有相同的信息。
【问题讨论】:
-
我们可以假设这些国家出现在
df_country1中的顺序与df_country2中的顺序完全相同吗?我们可以假设两个数据框的行数完全相同吗?
标签: python python-3.x pandas nan