简体   繁体   English

如果Nan,熊猫会将列值移动到同一列的第一行

[英]Pandas move column values to first row within same column if Nan

How do I go from this df , 我如何从这个df转到

     c0   c1   c2   c3 
0  5.99  NaN  NaN  NaN  
1   NaN   12  NaN  NaN                
2   NaN  NaN   44  NaN               
3   NaN  NaN  NaN   49

to this df , 为此df

     c0   c1   c2   c3 
0  5.99   12   44   49  

That is, 那是,

  • Move all non-NaN values into first row 将所有非NaN值移到第一行
  • Keep all values in original column 将所有值保留在原始列中

Per column, there's just 1 non-NaN value. 每列只有1个非NaN值。

Use bfill with axis=0 and downcast='infer' + dropna : 使用bfillaxis=0downcast='infer' + dropna

df = df.bfill(axis=0, downcast='infer').dropna()

print(df)
     c0  c1  c2  c3
0  5.99  12  44  49

Without downcast='infer' you get all float data types: 如果不使用downcast='infer'您将获得所有float数据类型:

print(df.bfill(0).dropna())
     c0    c1    c2    c3
0  5.99  12.0  44.0  49.0

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

相关问题 将列名移动到 pandas 框架中的第一行 - move column names to first row in pandas frame Python/Pandas:如果值为 NaN 或 0,则用同一行内下一列的值填充 - Python/Pandas: if value is NaN or 0 then fill with the value from the next column within the same row 如何在第一列的最后一行下移动熊猫列? - How to move pandas column under the last row of the first column? 为 pandas 过滤 function - 查看列中的 NaN 值 - Filtering function for pandas - VIewing NaN values within a column 在 pandas 列中,将重复数字替换为除第一个之外的 NAN 值 - Replace repetitive number with NAN values except the first, in pandas column Pandas - 用行值的总和(如果总和是偶数)或 NaN(如果是奇数)追加列 - Pandas - append column with sum of row values (if sum is even), or NaN (if odd) 根据pandas中的另一个Column和Row填写NaN值 - Filling in NaN values according to another Column and Row in pandas Pandas 基于另一个 dataframe 将多个列和行值设置为 nan - Pandas Set multiple column and row values to nan based on another dataframe 熊猫用列值填充NaN - Pandas Fill NaN with Column Values Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM