【问题标题】:Filling rows of the dataframe with calculation results用计算结果填充数据框的行
【发布时间】:2017-09-09 19:51:15
【问题描述】:

我有一个大的 txt 文件,如下所示:example

文件中有五列:代码、类型、日期、数量、计时。

我正在创建一个包含八列的新文件:代码、类型、ddc、ond、日期、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"

例如,如果我在文件“myfile.txt”中有时间 = 17,我会在我的新文件中创建包含八列的新行,其中在列“代码”、“类型”、“日期”中我从旧文件,在“nd”列中,我从“数量”列复制值。我需要做这17次。在“ond”列中,我需要指出天数。在“date_n”列中,我分配天数。同时,我想念休息日和假期。在“timing_с”和“ft”列中,我做了一些计算,我会尝试自己写在代码中。我需要将结果写入一个新文件。 Here is an example for the case if timing = 17, code = 0, type = dgv, date = 02.12.2016 , quantity = 2这是随机行。

在原始文件中,Timing 列中有几个不同的值。我认为如果可能,如果我有一个示例,我可以对其他值执行此操作,则需要使用条件来完成。

【问题讨论】:

    标签: python python-3.x csv pandas dataframe


    【解决方案1】:

    使用reindex_axis 创建新列并使用布尔切片减少到只有'Timing'17 的地方

    df1 = df[df.Timing == 17].reindex_axis(columns, axis=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 2021-09-13
      • 2022-08-09
      相关资源
      最近更新 更多