【发布时间】:2020-09-19 06:25:01
【问题描述】:
我正在尝试在 pandas 中使用 .loc 迭代地选择 500 行和所有列。但是,最终写入的文件最多只能显示 12 行数据。前 5 行和后 5 行。 写入文件中不显示中间数据。需要做什么才能显示所有 500 行???
以下是代码
"""
Program to
1. load data from a file. Skip the first 52 rows of data from a tab-separated file.
2. Select 500 rows and all columns from the dataset. Write this file.
3. Use the next name for saving the next set of 500 rows and iterate this process for 20 times
"""
import pandas as pd
import numpy as np
import os
import datetime
import shutil
import math
import datetime
b='b'
b=pd.read_csv('Oct4_3.dat',delimiter='\t',skiprows=52)
c=0
d=500
for i in range (1,20):
a='a'
filename = 'a'+str(i)+'.dat'
a=a+str(i)
print(a)
a=b.loc[c:d,'# V UG']
f = open(filename,'a+')
f.write(" %s \n"%(a))
c=d+1
d=d+500
f.close()
print('done')
代码输出生成 20 个文件。一个特定的文件如下所示
【问题讨论】:
标签: python pandas pandas-loc