Python Forum
Is there a more elegant way to concatenate data frames?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a more elegant way to concatenate data frames?
#1
Hi, I'm picturing a script that will include in a loop everything after the setting of "today" and before the "print" prototyped in the code below. The loop will read symbols from a notepad file line by line. I would substitute the hardcoded Ticker settings you see below with the symbol just read from notepad. If the record read is the first, the download call where AAPL is now a placeholder would be called. Otherwise the placeholder for the WMT call would be executed.

The download method allows passing multiple tickers in a call but long term I think this will be more flexible. From what I can tell, if you don't pass multiple symbols in a call, you don't get a column for Ticker.

Can I make this more elegant? It seems awkward.

import yfinance as yf
import pandas as pd
from datetime import date

today = date.today()

Ticker="AAPL"
data1 = yf.download(Ticker, start="2023-05-01", end=today).round(2)
data1["Ticker"]=Ticker

Ticker="WMT"
data2 = yf.download(Ticker, start="2023-05-01", end=today).round(2)
data2["Ticker"]=Ticker

data1=[data1,data2]
data1 = pd.concat(data1)

print(data1)
Reply


Messages In This Thread
Is there a more elegant way to concatenate data frames? - by db042190 - Jun-12-2023, 09:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 497 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,219 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  How to map two data frames based on multiple condition SriRajesh 0 1,518 Oct-27-2021, 02:43 PM
Last Post: SriRajesh
  Concatenate str JohnnyCoffee 2 2,968 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  More elegant way to remove time from text lines. Pedroski55 6 4,012 Apr-25-2021, 03:18 PM
Last Post: perfringo
  Concatenate two dataframes moralear27 2 1,915 Sep-15-2020, 08:04 AM
Last Post: moralear27
  Moving Rows From Different Data Frames JoeDainton123 1 4,399 Aug-06-2020, 05:19 AM
Last Post: scidam
  Comparing Items Different Data Frames With a WHILE Loop JoeDainton123 1 1,997 Jul-30-2020, 04:11 AM
Last Post: scidam
  merging data frames sportcardinal 0 1,230 Jun-30-2020, 12:21 AM
Last Post: sportcardinal
  can only concatenate str (not "int") to str gr3yali3n 6 4,175 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020