简体   繁体   English

如何在一列中使用来自其他数据帧的所有非 NaN 值创建新的 DataFrame

[英]How to create new DataFrame with all non-NaN values from other DataFrames in one column

I have two DataFrames ;我有两个DataFrames df1 and df2 and they both contain mostly NaN values. df1df2它们都包含大部分NaN值。 They have non-NaN values at the same locations in the data set.它们在数据集中的相同位置具有非 NaN 值。 Based on these two DataFrames , I would like to create df with a column for all values from df1 and one column for df2 , so that I have value pairs from both DataFrames .基于这两个DataFrames ,我想为df1中的所有值创建一列,为df2 DataFrames df值对。

Here I created a simple example:这里我创建了一个简单的例子:

df1:
            01K 02K 03K 04K
Dates               
2021-01-01  NaN NaN NaN NaN
2021-01-02  NaN 2.5 NaN NaN
2021-01-03  NaN NaN 4.1 NaN
2021-01-04  8.2 NaN 9.0 NaN
2021-01-05  NaN 1.2 NaN NaN
2021-01-06  NaN NaN NaN NaN

df2:
            01K 02K 03K 04K
Dates               
2021-01-01  NaN NaN NaN NaN
2021-01-02  NaN 0.6 NaN NaN
2021-01-03  NaN NaN 0.4 NaN
2021-01-04  0.1 NaN 0.2 NaN
2021-01-05  NaN 0.2 NaN NaN
2021-01-06  NaN NaN NaN NaN

df:
    df1 df2
0   8.2 0.1
1   2.5 0.6
2   1.2 0.2
3   4.1 0.4
4   9.0 0.2

For reproducibility:为了重现性:

df1 = pd.DataFrame({
    'Dates':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06'],
    '01K':[np.nan, np.nan, np.nan, 8.2, np.nan, np.nan], 
    '02K':[np.nan, 2.5, np.nan, np.nan, 1.2, np.nan], 
    '03K':[np.nan, np.nan, 4.1, 9.0, np.nan, np.nan], 
    '04K':[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]}) 
df1 = df1.set_index('Dates')

df2 = pd.DataFrame({
    'Dates':['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05', '2021-01-06'],
    '01K':[np.nan, np.nan, np.nan, 0.1, np.nan, np.nan], 
    '02K':[np.nan, 0.6, np.nan, np.nan, 0.2, np.nan], 
    '03K':[np.nan, np.nan, 0.4, 0.2, np.nan, np.nan], 
    '04K':[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]}) 
df2 = df2.set_index('Dates')

Thanks a lot for your suggestions非常感谢您的建议

You can use stack to get rid of the nan's:您可以使用stack来摆脱 nan 的:

pd.DataFrame({'df1': df1.stack(), 'df2': df2.stack()})

output: output:

                df1  df2
Dates                   
2021-01-02 02K  2.5  0.6
2021-01-03 03K  4.1  0.4
2021-01-04 01K  8.2  0.1
           03K  9.0  0.2
2021-01-05 02K  1.2  0.2

暂无
暂无

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

相关问题 如何通过每行的非NAN值计数来提取此数据框中的所有非NAN值 - How to extract all non-nan values in this dataframe by the non-nan values count of each row 如何用 1 替换数据帧的所有非 NaN 条目,用 0 替换所有 NaN - How to replace all non-NaN entries of a dataframe with 1 and all NaN with 0 检查 dataframe 中的所有行是否具有一个或零个非 NaN 值的最快方法是什么? - what's the fastest way to check that all rows in a dataframe have one or zero non-NaN values? 如何使用pandas选择所有非NaN列和非NaN最后一列? - How to select all non-NaN columns and non-NaN last column using pandas? 从pandas数据帧中的多行中提取非nan值 - To extract non-nan values from multiple rows in a pandas dataframe 如何以Python方式返回具有唯一列Pandas DataFrame的非nan值的df - How to return df with non-nan values of unique column Pandas DataFrame Pythonically 如何获取熊猫数据框中所有非NaN项的行,列索引 - How to get row, column indices of all non-NaN items in Pandas dataframe 如何返回 dataframe 每个月每列中的最后一个非 NaN 值? - How to return a dataframe with the last non-NaN values in each column for each month? 如何用系列替换数据帧的所有非 NaN 条目? - How to replace all non-NaN entries of a dataframe with a Series? 从两个数据帧创建新的 dataframe。 一个df包含列索引,另一个df包含值 - Create new dataframe from two dataframes. One df contains column indices the other df the values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM