Return 整行并附加到数据框中的值
Return entire row and append to value in a dataframe
我正在尝试编写一个函数,逐行搜索数据框以查找列中的值,然后如果在任何行中找到该值,则将整行附加到该值的右侧。
Dataframe1
Col1 Col2 Col3 Lookup
400 60 80 90
50 90 68 80
我想要的是以下数据框:
Dataframe 2
Lookup Col1 Col2 Col3
90 50 90 68
80 400 60 80
非常感谢任何帮助。
你可以试试这个;
df1 = df.iloc[:,0:-1]
new = pd.DataFrame()
for val in df['Lookup']:
s = df1[df1.eq(val).any(1)]
new = new.append(s,ignore_index = True)
new.insert(0,'Lookup',df['Lookup'])
print(new)
# Lookup Col1 Col2 Col3
# 0 90 50 90 68
# 1 80 400 60 80
我正在尝试编写一个函数,逐行搜索数据框以查找列中的值,然后如果在任何行中找到该值,则将整行附加到该值的右侧。
Dataframe1
Col1 Col2 Col3 Lookup
400 60 80 90
50 90 68 80
我想要的是以下数据框:
Dataframe 2
Lookup Col1 Col2 Col3
90 50 90 68
80 400 60 80
非常感谢任何帮助。
你可以试试这个;
df1 = df.iloc[:,0:-1]
new = pd.DataFrame()
for val in df['Lookup']:
s = df1[df1.eq(val).any(1)]
new = new.append(s,ignore_index = True)
new.insert(0,'Lookup',df['Lookup'])
print(new)
# Lookup Col1 Col2 Col3
# 0 90 50 90 68
# 1 80 400 60 80