简体   繁体   English

将最后一个非 nAn 值替换为数据帧的最后一列

[英]Replacing the last non-nAn value to the last column of the dataframe

My current dataframe is this:我当前的数据框是这样的:

df.head()

Output:输出:

Name姓名 Gender性别 Country国家 Year
Adam亚当 Male男性 America美国 1993 1993
Betty贝蒂 1968 1968年 NaN NaN
Calvin卡尔文 NaN 1995 1995 NaN
Debra黛布拉 Female女性 2000 2000 NaN

I would like to shift all the last non-NaN values to the last column of the dataframe.我想将所有最后的非 NaN 值转移到数据框的最后一列。

  • Every row will have the year in the last non-NaN column of the dataframe每一行在数据框的最后一个非 NaN 列中都有年份
  • What every row definitely has is the 'Name' and 'Year' value每行肯定有的是“名称”和“年份”值
  • The 'Year' value could be either in Gender or Country column “年份”值可以在“性别”或“国家”列中

So basically I need a code that populates this output:所以基本上我需要一个填充这个输出的代码:

Output:输出:

Name姓名 Gender性别 Country国家 Year
Adam亚当 Male男性 America美国 1993 1993
Betty贝蒂 NaN NaN 1968 1968年
Calvin卡尔文 NaN NaN 1995 1995
Debra黛布拉 Female女性 NaN 2000 2000

Thank you for your help谢谢您的帮助

Use ffill on index axis because Year is your last column and the last valid value of a row is a year.在索引轴上使用ffill因为Year是您的最后一列,而行的最后一个有效值是一年。

df['Year'] = df.ffill(axis=1)['Year'].astype(int)
print(df)

# Output
     Name  Gender  Country  Year
0    Adam    Male  America  1993
1   Betty    1968      NaN  1968
2  Calvin     NaN     1995  1995
3   Debra  Female     2000  2000

To be safe, you can use pd.to_numeric to remove values where year is not defined for a row:为了安全起见,您可以使用pd.to_numeric删除未为行定义年份的值:

df['Year'] = pd.to_numeric(df.ffill(axis=1)['Year'], errors='coerce')
print(df)

# Output
     Name  Gender  Country    Year
0    Adam    Male  America  1993.0
1   Betty    1968      NaN  1968.0
2  Calvin     NaN     1995  1995.0
3   Debra  Female     2000  2000.0

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

相关问题 根据列名和最后一个非NaN值压缩pandas数据帧 - Compress pandas dataframe based on column name and last non-NaN value 熊猫:上一次列具有非Nan值的情况 - Pandas: Last time when a column had a non-nan value 如何使用pandas选择所有非NaN列和非NaN最后一列? - How to select all non-NaN columns and non-NaN last column using pandas? 如何返回 dataframe 每个月每列中的最后一个非 NaN 值? - How to return a dataframe with the last non-NaN values in each column for each month? 获取pandas中每个月的最后一个非NaN值 - Get last non-NaN value for each month in pandas pd.DataFrame:获取两个日期时间之间每个 id 的平均值; 如果是 NaN,则获取最后一个非 NaN 值 - pd.DataFrame: get average value per id between two datetimes; if NaN, get last non-NaN value 获取 pandas dataframe 中列的先前非 NaN 值来计算 - Get previous non-NaN value of a column in a pandas dataframe to calculate 两个日期时间之间的平均值; 如果是 NaN,则获取最后一个非 NaN 值 - Mean between two datetimes; if NaN, get last non-NaN value 获取排序的numpy矩阵或pandas数据帧的最后一个非nan索引 - Getting the last non-nan index of a sorted numpy matrix or pandas dataframe 在排序的多维numpy数组中沿轴查找最后一个非NaN值 - Find last non-NaN value along axis in sorted multi-dimensional numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM