python - 在单个 csv 文件上创建具有连续值的列
问题描述
我有一个大的 csv 文件,我将其拆分为六个单独的文件。我正在使用“for 循环”来读取每个文件并创建一列,其中值递增一。
whole_file=['100Hz1-raw.csv','100Hz2-raw.csv','100Hz3-raw.csv','100Hz4-raw.csv','100Hz5-raw.csv','100Hz6-raw.csv']
first_file=True
for piece in whole_file:
if not first_file:
skip_row = [0] # if it is not the first csv file then skip the header row (row 0) of that file
else:
skip_row = []
V_raw = pd.read_csv(piece)
V_raw['centiseconds'] = np.arange(len(V_raw)) #label each centisecond
有没有一种聪明的方法来做我想做的事。
解决方案
将最后一个值存储为厘秒并从那里开始计数:
whole_file=['100Hz1-raw.csv','100Hz2-raw.csv','100Hz3-raw.csv','100Hz4-raw.csv','100Hz5-raw.csv','100Hz6-raw.csv']
first_file=True
## create old_centiseconds variable
old_centiseconds = 0
for piece in whole_file:
if not first_file:
skip_row = [0] # if it is not the first csv file then skip the header row (row 0) of that file
else:
skip_row = []
V_raw = pd.read_csv(piece)
# add old_centiseconds onto what you had before
V_raw['centiseconds'] = np.arange(len(V_raw)) + old_centiseconds #label each centisecond
# update old_centiseconds
old_centiseconds += len(V_raw)
推荐阅读
- javascript - 幻灯片的动画不起作用
- javascript - html表格的多个过滤器
- ios - 两个 ViewController 展开问题
- java - GAE fetchAsync 会比请求更长寿吗?
- javascript - 当 angularjs 抛出错误时,如何使 TestCafe 测试失败?
- node.js - 如何将扩展运算符与“this”一起使用?
- javascript - 在 ASP.NET MVC 项目中使用类库中的 Javascript 文件
- html - 移动设备上的文本样式行高不起作用(WordPress)
- javascript - 根据现有对象数组的几个属性返回新的对象数组
- javascript - 与之前的状态相比,React SetState 如何更新状态