如果 xlsx sheet 中存在该值而不知道索引号,如何获取该值?
How to get the value if the value is present in the xlsx sheet without knowing index number?
我有一个非结构化 Xslx 文件。如果 sheet 中存在值,我想获取整行。例如
A B C D F
abc 10 24 32 54
cdf 9 10 34 98
mgl 11 90 21 98
fgd 1 9 2 10
我想获取 sheet 中是否存在 10 个值以获得完整的行值
输出=>
abc 10 24 32 54
cdf 9 10 34 98
fgd 1 9 2 10
感谢您的贡献
您可以使用 pandas.DataFrame.isin followed by pandas.DataFrame.any:
df[df.isin([10]).any(axis = 1)]
A B C D F
0 abc 10 24 32 54
1 cdf 9 10 34 98
3 fgd 1 9 2 10
如果每行至少有一个 True
,请使用 DataFrame.eq
with DataFrame.any
进行测试:
df = pd.read_excel('file.xlsx')
df1 = df[df.eq(10).any(axis=1)]
或者:
df1 = df[(df == 10).any(axis=1)]
print (df1)
A B C D F
0 abc 10 24 32 54
1 cdf 9 10 34 98
3 fgd 1 9 2 10
我有一个非结构化 Xslx 文件。如果 sheet 中存在值,我想获取整行。例如
A B C D F
abc 10 24 32 54
cdf 9 10 34 98
mgl 11 90 21 98
fgd 1 9 2 10
我想获取 sheet 中是否存在 10 个值以获得完整的行值
输出=>
abc 10 24 32 54
cdf 9 10 34 98
fgd 1 9 2 10
感谢您的贡献
您可以使用 pandas.DataFrame.isin followed by pandas.DataFrame.any:
df[df.isin([10]).any(axis = 1)]
A B C D F
0 abc 10 24 32 54
1 cdf 9 10 34 98
3 fgd 1 9 2 10
如果每行至少有一个 True
,请使用 DataFrame.eq
with DataFrame.any
进行测试:
df = pd.read_excel('file.xlsx')
df1 = df[df.eq(10).any(axis=1)]
或者:
df1 = df[(df == 10).any(axis=1)]
print (df1)
A B C D F
0 abc 10 24 32 54
1 cdf 9 10 34 98
3 fgd 1 9 2 10