简体   繁体   English

用值分组两列以获得第三列

[英]Grouping two columns with values to get a third column

you'll find two columns Name and N - for most entries both name and N are same 您会发现两列Name和N-对于大多数条目,name和N都是相同的

but there are cases where N is missing when Name is present and vice versa 但是在某些情况下,当存在Name时会丢失N,反之亦然

Group the columns such that k I have one resultant column that has all values 对列进行分组,以使k I具有一个包含所有值的结果列

Example : 范例:

Col1 Col2 value.... 

Adam   nan   334

John    nan   56

nan    Michael  90

Result : 结果:

Col1 value.... 

Adam   334

John    56

Michael  90

try this : 尝试这个 :

for index, row in df.iterrows() :
  if not isinstance(df['col1'][index],str) : 
    df['col1'][index] = df['col2'][index]

knowing that nan is a float, if it finds that the value at 'col1' is nan it will take the value at 'col2' 知道nan是一个浮点数,如果它发现'col1'的值是nan,它将取'col2'的值

or using apply 或使用套用

df['B'] = df.apply(lambda x : x['C'] if not isinstance(x['B'],str) else x['B'] ,axis= 1)

new_df = df.delete('C',axis=1)

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

相关问题 大熊猫:按两列分组,然后按第三列的值排序 - pandas: Grouping by two columns and then sorting it by the values of a third column 按两列分组并根据两列条件添加第三列 - Grouping by two columns and add a third column based on a condition of two columns 有没有办法在使用 pandas 按第三列中的值分组时将两列中的值相乘? - Is there a way to multiply the values in two columns while grouping by values in third column using pandas? 根据第三列中的条件获取两个不同列的值 - Get values of two different columns based on a condition in a third column 通过对两列进行分组并对第三列数据python求和来进行筛选 - Filter by grouping two columns and sum third column data python 比较两列中的值并提取 dataframe 中第三列的值 - Compare the values in two columns and extract the values of a third column in a dataframe Dataframe - 对于每一行,比较两列的值,匹配时获取第三列的值 - Dataframe - for each row, compare values of two columns, get value of third column on match groupby 两列并计算第三列中的唯一值 - groupby two columns and count unique values from a third column Python:如果两列具有相同的值,则为第三列的和值 - Python: sum values of the third column if two columns have the same value 根据pandas中的第三列保留两列之间的值 - Keep values of between two columns based on third column in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM