简体   繁体   English

使用熊猫使用另一列中的值填充特定列中的空白行

[英]Fill Blank Row in a specific column with value from another column using pandas

If I have the following pandas dataframe如果我有以下熊猫数据框

      A     B
0     Cat   Dog
1     Mouse Mouse
2     Fish  "Blank"

How do I parse through column B, identify that there is a blank value and then if there is a blank value fill in the blank with "Fish", so the desired data frame is:我如何通过B列解析,确定有一个空白值,然后如果有一个空白值用“鱼”填充空白,所以所需的数据框是:

      A     B
0     Cat   Dog
1     Mouse Mouse
2     Fish  Fish

You could try this -你可以试试这个——

import numpy as np
df['B'] = np.where(df['B'] == 'Blank', df['A'], df['B'])
  • If only want to check from 'Blank' elements in column B:如果只想检查 B 列中的“空白”元素:

df[df['B'] == 'Blank'] = 'Fish'

  • Check to entire dataframe:检查整个数据帧:

df[df=='Blank'] = 'Fish'

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

相关问题 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas MSSQL:如果有条件,则使用另一列中的值有条件地填充该列 - MSSQL: Conditionally Fill in Column With Value From Another Column If Blank Pandas 用行值填充列 - Pandas fill column with row value 如何使用 Python Pandas 从 1 行中的特定列获取值? - How to get value from specific column in 1 row using Python Pandas? 如何从另一列及以上行中的值填充 pandas dataframe 中的 nan 值? - How to fill nan value in pandas dataframe from value in another column and above row? 从另一列列表中的特定值填充一个数据框列 - Fill one Dataframe Column from specific value in list of another column 如何使用来自另一个 dataframe 列的值填充 pandas dataframe 列 - How to fill a pandas dataframe column using a value from another dataframe column 熊猫:从另一列填充分区的第一行? - Pandas: Fill up first row in partition from another column? 根据熊猫中的行匹配,用另一个DataFrame中的值有条件地填充列 - Conditionally fill column with value from another DataFrame based on row match in Pandas 在 Pandas 中查找包含另一列行中特定值的列名 - Find column name in Pandas that contains a specific value in the row from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM