2018-05-17 01:35

Python提取一列数据，检测其是否在一个范围内..

20

'01'<......<'05'

`````` import pandas
from pandas import DataFrame
f=open('F:\\FCD\\11-12总时间.txt')
file=f.readlines()
a=0
n=0
c=0
d=0
e=0
f=0
g=0
h=0
i=0

L=[]
for line in file:
L.append(line.split(':'))
time=DataFrame(L)
timemin=time[1]
for i in range(len(timemin)):
if '1'<timemin[i]<'5':
a=a+1
if '5'<timemin[i]<'10':
n=n+1
if '10'<timemin[i]<'15':
c=c+1
if '15'<timemin[i]<'20':
d=d+1
if '20'<timemin[i]<'25':
e=e+1
if '25'<timemin[i]<'30':
f=f+1
if '30'<timemin[i]<'35':
g=g+1
print(a)
print(n)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
``````

`````` print(a)
print(n)
print(c)
print(d)
print(e)
print(f)
print(g)
print(h)
print(i)
6648
0
2237
1188
611
349
190
0
18389
``````

`````` from pandas import DataFrame
import pandas as pd
a=0
.......

L=[]
M=[]
afile=open('F:\FCD\one\odd.txt')
bfile=open('F:\FCD\one\even.txt')

for line in afile:
L.append(line.split(','))
data=pd.DataFrame(L)
na=data[4]
nac=data[5]

for bline in bfile:
M.append(bline.split(','))
datab=pd.DataFrame(M)
nb=datab[4]
nbc=datab[5]

for i in range(len(data)):
if '113.85'<na[i]<'114.1' and '22.4'<nac[i]<'22.6' and '113.85'<nb[i]<'114.1' and '22.4'<nbc[i]<'22.6':
a=a+1
..........

print(a)
........
``````

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

3条回答

• 评论怎么消失了？在for语句里面改成这样子试试，把所有要对比的数据都变成数来进行对比
if 1<float(timemin[i])<5:
a=a+1
if 5<float(timemin[i])<10:
n=n+1
if 10<float(timemin[i])<15:
c=c+1
if 15<float(timemin[i])<20:
d=d+1
if 20<float(timemin[i])<25:
e=e+1
if 25<float(timemin[i])<30:
f=f+1
if 30<float(timemin[i])<35:
g=g+1

点赞 评论 复制链接分享
• 如果这样还不行，就只能是数据本身的问题了，要拿数据出来看一看

点赞 评论 复制链接分享
• 你把你的11-12总时间.txt文件贴出来看下

点赞 评论 复制链接分享