Python 如何获取熊猫中的行索引?
我有一个如下所示的数据框Python 如何获取熊猫中的行索引?,python,pandas,Python,Pandas,我有一个如下所示的数据框 customers ... Date 2006-01-03 98 ... 2006-01-04 120 ... 2006-01-05 103 ... 2006-01-06 95 ... 2006-01-09
customers ...
Date
2006-01-03 98 ...
2006-01-04 120 ...
2006-01-05 103 ...
2006-01-06 95 ...
2006-01-09 103 ...
我想得到客户数超过100的行并打印出来
for x in range(len(df)):
if df['customers'].iloc[x] > 100:
print(df['customers'].iloc[x])
但我不知道如何打印出符合条件的行的日期(索引)。我的目标是这样打印:
2006-01-04
120
2006-01-05
103
2006-01-09
103
考虑使用:
要获得指定的确切输出格式,请迭代query()
结果:
for date, customer in df.query('customers > 100').values:
print(date)
print(customer)
2006-01-04
120
2006-01-05
103
2006-01-09
103
df[df['customer']>100]将完成此工作。。。
虽然您可以使用循环在stackoverflow上找到许多类似的答案,但您可以使用df.index[x]在输出中获取数据
for x in range(len(df)):
if df['customers'].iloc[x] > 100:
print(df.index[x])
print(df['customers'].iloc[x])
2006-01-04
120
2006-01-05
103
2006-01-09
103
Date列是此数据帧的索引。您是否碰巧知道如何获得符合条件的每一行客户价值的日期?我不确定我是否理解您的要求。如果
Date
是索引,则query()
函数的工作方式相同。如果您只希望日期与您的条件相匹配(而不是OP desired output所显示的日期),那么只需提取索引:df.query('customers>100')。index.value
for x in range(len(df)):
if df['customers'].iloc[x] > 100:
print(df.index[x])
print(df['customers'].iloc[x])
2006-01-04
120
2006-01-05
103
2006-01-09
103