我使用for语句和熊猫支持的一些api来加载一些文件,我创建了一个临时数据列表,最后创建了一个包含该列表数据的数组,这个数组可以在我一次键入几个文件时创建。 ,但是当我导入多个文件时,会创建一个奇怪的数组。确保所有文件的列数和行数均相同。我该怎么办?
Py3.6,最新的熊猫
当我一次加载几个文件时
import numpy as np
dirList=os.listdir(r'C:\Users\Administrator\Desktop\New folder\SecData\renzaorou\sec20190527')
priceList=[]
for i in dirList[:3]: # few files
for i in dirList: # all files
print(i)
df=pd.read_excel('C:\\Users\\Administrator\\Desktop\\New folder\\SecData\\renzaorou\\sec20190527\\'+i)
# row_price = df['price'].tolist()
# print(df)
# break
priceList.append(df['price'].tolist())
np_price=np.array(priceList)
加载少量文件时的预期输出
array([[ 0. , 0. , 0. , ..., 6.32, 6.32, 6.32],
[ 0. , 0. , 0. , ..., 3.45, 3.45, 3.45],
[ 0. , 0. , 0. , ..., 11.51, 11.51, 11.51]])
然后在我加载所有文件时
array([list([0.0, 0.0, 0.0, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.19, 6.1.........
加载所有文件时,我希望输出正常
答案 0 :(得分:0)
似乎您只需要将numpy数组追加到列表中即可。尝试:
import numpy as np
dirList=os.listdir(r'C:\Users\Administrator\Desktop\New folder\SecData\renzaorou\sec20190527')
priceList=[]
for i in dirList: # all files
print(i)
df=pd.read_excel('C:\\Users\\Administrator\\Desktop\\New folder\\SecData\\renzaorou\\sec20190527\\'+i)
priceList.append(df['price'].values)
np_price=np.array(priceList)
如果price
列不是数字列,只需替换
priceList.append(df['price'].values)
使用
priceList.append(pd.to_numeric(df['price']).values)