假设我有一个包含整数和float混合的列,我需要在该列中运行一个循环来检查所有值是否都是数值。非数字值,将该单元格的索引转储到空白列表中。
像这样的专栏:
DESCRIPTION RATE
79th St & Private Dr - 5022 W 1.5
79th St & Allison Ave 1.5
Allison Ave & 81st St 1
81st St & Norfolk St 1
Norfolk St & Private Dr - 8178 N 1
Norfolk St & 84th St 1
Moller Rd & 86th St 1.5
Zionsville Rd & 86th St 1.5
我写了这样的代码,但是newlist循环不起作用。
import pandas as pd
import numpy as np
df = pd.read_csv("C:\\Python34\\python scripts\\BNSF\\data\\data.csv") #read csv
col=df.columns #read header
print(col)
r=df.shape[0] #gives number of row count
c=df.shape[1] #gives number of col count
print(r, c)
typ=df.dtypes
print(typ)
newlist=[] #creates blank list
for i in range(r,1):
if df.iloc[i,1].dtype == np.int64:
pass
else:
print(df.iloc[i,1])
newlist.append(df.iloc[i,1])
print(newlist)
需要在该循环内进行更改。请帮忙。
答案 0 :(得分:1)
大熊猫的美妙之处在于你经常可以避免使用循环。可以通过执行模1操作并检查零等价%1==0
来生成变量intcheck
中的真/假值的数据帧来实现将数据分类为整数/非整数。然后只获取原始DataFrame中整数的True
行。在前面放置~
会反转布尔值以获得非整数值。
import pandas as pd
d =[["79th St & Private Dr - 5022 W" ,1.5],
["79th St & Allison Ave" ,1.5],
["Allison Ave & 81st St" ,1 ],
["81st St & Norfolk St" ,1 ],
["Norfolk St & Private Dr - 8178 N" ,1 ],
["Norfolk St & 84th St" ,1 ],
["Moller Rd & 86th St" ,1.5],
["Zionsville Rd & 86th St" ,1.5]]
df = pd.DataFrame(d, columns=['DESCRIPTION','RATE'])
df = df.set_index('DESCRIPTION')
intcheck = (df.RATE%1)==0
print(df[intcheck])
# RATE
# DESCRIPTION
# Allison Ave & 81st St 1.0
# 81st St & Norfolk St 1.0
# Norfolk St & Private Dr - 8178 N 1.0
# Norfolk St & 84th St 1.0
print(df[~intcheck])
# RATE
# DESCRIPTION
# 79th St & Private Dr - 5022 W 1.5
# 79th St & Allison Ave 1.5
# Moller Rd & 86th St 1.5
# Zionsville Rd & 86th St 1.5