I have dataframe defined as below and want to filter using variable name ft as in below code:
df = pd.DataFrame([[1,2], [3,4]], columns=['a', 'b'])
ft=["a"]
dt=df[df[ft]==3]
print(dt)
I get below result:
a b
NaN NaN
3.0 NaN
while actual result should have been only 2nd row
a b
3 4
I dont want to use column name, but variable.
thanks in advance for any help
CodePudding user response:
Or if you aren't allowed to change the ft
variable, do:
>>> df[df[ft].squeeze() == 3]
a b
1 3 4
>>>
CodePudding user response:
Use scalar ft="a"
for Series
df['a']
instead one column DataFrame df[['a']]
if ft=["a"]
.
If need test index use:
ft = 3
dt = df.loc[ft]
If not sure if exist:
ft = 3
dt = df[df.index == ft]
For first column use:
ft = 3
dt = df[df.iloc[:, 0] == ft]