[英]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.