0 1 2
0 this is 1.0
1 my book 1.0
2 book this 1.0
3 is my 0.5
4 is interesting 0.5
我的数据框看起来像这样,但是它是动态的。因此它也可能看起来像
0 1 2 3
0 this is interesting 0.5
1 is my book 1.0
2 this is my 0.5
3 my book this 1.0
4 book this is 1.0
现在,我想从最后一列获取浮点值,以将前三列的值作为元组给出。像
("this","is")
("this","is","interesting")
应该分别给我1.0和0.5的值。请帮我解决这个问题。
当前我正在使用
row = df[(df["0"] == "is") & (df["1"] == "my")]
但它不是动态的。
答案 0 :(得分:0)
我不确定这是否是您想要的,看看是否可以根据您的需要进行修改:
f = ("this","is","interesting")
#condition checks if words can be found in the dataframe
#looks for rows that has all the words
#and converts to an array of True or False
cond = df.iloc[:,:-1].isin(f).all(axis=1).to_numpy()
#filter the dataframe, to get the values from the last column
#where condition is True
#iloc works with an array or list of Booleans,
#it cannot reference a series label,
#hence then need to convert the cond to a list or array
df.iloc[cond,-1]
0 0.5
Name: 3, dtype: float64
当然,您可以将结果分配给一列...但是我不确定这是否是您的