python - 如何解决 AttributeError:'list' 对象没有属性 'shape'?
问题描述
此代码旨在根据某些条件将文件分成多个。
我在循环之前 print(temp.shape[0]) ,它可以工作。但它显示 n = temp.shape[0] AttributeError: 'list' object has no attribute 'shape' 在循环中。我想知道为什么会这样以及如何纠正它。
import pandas as pd
limit = 3
G0 = 7.75e-05
v=0.1
step_size = 2e-3
splitcounts=200
data1 = pd.read_csv(\
'C:\\Data Analysis\\Data.txt',\
sep=',',names=['Time1','Current','Voltage','Distance', 'Time2'])
nrows1 = data1.shape[0]
temp=pd.DataFrame(columns=['Time','Current'])
print(temp.shape[0])#this is right when I run the code
for i,j in zip(range(0, int(nrows1)),range(0, int(splitcounts))):
if 0<data1.at[i,'Current']<0.000075:
temp=temp.append(data1.loc[i,0:2])
else:
n = temp.shape[0] #but it show 'AttributeError: 'list' object has no attribute 'shape''
if n>5:
temp.to_csv("C:\\Data Analysis\\Datas/%04d.csv" %j,\
sep=',',index=False, header=None)
else:
temp = []
解决方案
temp = pd.DataFrame(temp)
n = temp.shape[0]
添加这个可以将列表更改为数组。错误已解决,但我仍然无法将数据拆分为我需要的数据。然后我添加代码如下:
temp = temp.drop(index=temp.index)
它运作良好。整个正确的代码是:
for i in range(0, int(nrows1)):
if 0<data1.at[i,'Current']<0.000075:
temp = pd.DataFrame(temp)
temp=temp.append(data1.iloc[i,0:2])
else:
#temp = np.array(list, dtype=float)
temp = pd.DataFrame(temp)
n = temp.shape[0]
if n>20:
#print(temp)
temp.to_csv("C:/Datas/%04d.csv" %i,\
sep=',',index=False, header=None)
temp = temp.drop(index=temp.index)
else:
temp = temp.drop(index=temp.index)
推荐阅读
- sql - 我的 SQL 查询哪里出错了?
- javascript - word导入后括号的意义是什么
- big-o - 证明以下关于渐近符号的问题的正确方法是什么?
- python-3.x - 离散随机变量的有效抽样
- javascript - 未捕获的类型错误:无法在 lt (d3.v5.min.js:2) 处读取 null 的属性“样式”
- binary - 如何将 234.35 转换为二进制?
- ruby-on-rails - 使用 http 协议而不是 TCP 启动 rails 服务器
- php - Laravel 非严格验证
- vba - 如何使用 Userform 从特定行(比如 A8)值开始捕获数据
- php - 使用 PHP 将值绑定到 JSON 字符串