[英]Pandas Basics using Square Brackets
As seen by the code, I am trying to print out my table from the intervals of 0 to when hr1 in the column time is first equal to 1: 01 ( or 1:01 am). 正如代码所示,我试图从0的间隔打印出我的表,当列时间中的hr1首先等于1:01(或1:01 am)。 I am having a problem with how to implement when hr1 is equal to 1:01 in the square brackets.
当方法括号中hr1等于1:01时,我遇到了如何实现的问题。
import pandas as pd
table = pd.read_csv('2019-01-20.csv')
speed = table['speed_mph']
time = table['timestamp']
x = 1
for hr1 in time:
if x < 2:
print(table[0:(hr1='1:01')])
x += 1
Simplest would be to use index to find the first element equal to 1.01
最简单的方法是使用index来查找等于
1.01
的第一个元素
print(table[0:time.index('1:01')+1])
I've added one to the result of the index()
call to include that element. 我已经在
index()
调用的结果中添加了一个以包含该元素。 Note that if '1.01' is the last element in the list, this will yield an IndexError
. 请注意,如果'1.01'是列表中的最后一个元素,则会产生
IndexError
。
If the element is not found index()
will raise a ValueError
. 如果找不到元素,
index()
将引发ValueError
。
Note also that you should not use time
as a variable name, as this shadows the python built-in module of the same name. 另请注意,不应将
time
用作变量名,因为这会影响同名的python内置模块。
I second the comment above, as well. 我也是上面的评论。 Simply guessing at what the syntax might be will cause a lot of wasted time.
简单地猜测语法可能会导致大量的浪费时间。 You may also find that you get something working by accident, which later on fails with a different data-set.
您可能还会发现您偶然发现了一些工作,后来因为不同的数据集而失败。
table.iloc[:table[table.timestamp == 'your timestamp'].index[0] + 1]
iloc切片索引,table [table.timestamp =='your timestamp'] .index [0]返回第一个匹配的索引,+ 1将其包含在结果中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.