用计算结果填充数据帧的行

2024-04-27 04:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很大的txt文件,看起来像这样:example

文件中有五列:代码、类型、日期、数量、时间。你知道吗

我正在创建一个包含八列的新文件:code、type、ddc、ond、date、timing、ft、nd。我需要把数据填进去。 下面是一个算法:

import pandas as pd
columns = ['code', 'type', 'date', 'ond', 'date_n', 'timing_с', 'ft', 'nd']

df = pd.read_csv("myfile.txt") #Here I read the source file
df1 = pd.DataFrame(columns=columns)

for index, row in df.iterrows(): 
    if row['Timing'] == 17: #Here I look what value is in the cell of the 
    #column "Timing"

例如,如果文件中的timing=17”myfile.txt文件,我在我的新文件中创建一个新行,有八列,其中在“code”、“type”、“date”列中,我从旧文件复制值,在“nd”列中,我从“quantity”列复制值。我需要把这17次。在“ond”一栏中,我需要指出天数。在“日期”一栏中,我要分配天数。同时,我也想念假期和休息日。在“计时”和“英尺”列中,我做了一些计算,我将尝试自己在代码中编写这些计算。我需要把结果写进一个新文件。 Here is an example for the case if timing = 17, code = 0, type = dgv, date = 02.12.2016 , quantity = 2这是一个随机行。你知道吗

在原始文件中,计时列中有几个不同的值。我认为这需要用条件来做,如果,也许,如果我有一个例子,我可以对其他值这样做。你知道吗


Tags: columns文件the代码txtdatehereexample