【问题标题】:Appending dataframes into one [duplicate]将数据帧附加到一个[重复]
【发布时间】:2022-02-20 14:53:57
【问题描述】:

我正在通过 for 循环处理数百个 excel 文件,并从每个文件生成一个新的数据框。最后,我需要将所有数据帧附加到一个文件中,其中只有第一个文件应该保留标题。 我正在使用下面的代码。请帮助我-

folder = r"C:\Users\Mohit\ALGO_TRADING\Play"

for file in os.listdir(folder):
    filepath = os.path.join(folder, file)
    data = pd.read_csv (filepath)
    data['Close_EMA_20'] = ta.EMA(data['Close'],20)
    data['Close_EMA_5'] = ta.EMA(data['Close'],5)
    data ['Position'] = np.nan
    pd.options.mode.chained_assignment = None
    
    for x in range (len (data)):
        if data['Close_EMA_5'].iloc[x] < data['Close_EMA_20'].iloc[x]:
            data ['Position'].iloc[x] = -1
        elif data['Close_EMA_5'].iloc[x] > data['Close_EMA_20'].iloc[x]:
            data ['Position'].iloc[x] = 1
        else:
            data ['Position'].iloc[x] = 0
            
        position_data = data[data['Position']!=0] #creating a new dataframe every after a loop
    
    Position_Data = position_data.append(position_data) #appending all dataframe into one. PROBLEM IS HERE
    
Position_Data.to_csv(r"C:\Users\Mohit\ALGO_TRADING\mohit.csv")

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    要附加两个数据帧,请使用pd.concat([df1, df2])

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 2021-12-12
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多