Python Forum
How to create a column with periodic entries?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a column with periodic entries?
#1
I'm trying to create a stock chart but with date on the x-axis, it's a black blur because the dataframe has 3407 entries.

So... what I'm thinking of doing is creating a new df column that has entries every n rows to allow it to be seen.

I tried this:
ES_skel = ES.drop(['Open','High','Low','Vol'],axis=1)
ES_skel.index = ES_skel["Date"].apply(lambda x: pd.Timestamp(x))

for i in range(len(ES_skel)):
    if i%5 == 0:
        ES_skel["Month_Year"] = ES_skel.Date #ES_skel.index.strftime('%Y-%m')

print(ES_skel.head(15))
This resulted in the new Month_Year column being populated with the date for every row. Adding this else clause:
    else: ES_skel["Month_Year"] = None   
resulted in None for every row in the new Month_Year column.

What happened? With the if condition being executed initially, why was the else condition executed when included? And why was execution done every row rather than every 5th row per the i%5 line?

Thanks!
Reply
#2
I think I know why this happened: whether or not i%5==0, the respective condition was executed for all cells in the column. Right? If I included the else clause and range(len(ES_skel)) led to the else clause being executed, then all cells in the column would show None.

This might also explain why the program took so long to run: for each i, it was writing and overwriting 3407 entries?

What I somehow need to do is call just the particular cell... maybe with .iloc[i] somewhere?
Reply
#3
This works:
ES_skel["Month_Year"]=np.nan
for i in range(len(ES_skel)):
    if i % 5 == 0:
        ES_skel.iloc[i,2] = ES_skel.iloc[i,0] 
My actual problem, however, seemed to be:
ax.plot(ES_skel['Month_Year'], ES_skel['Close']); plt.show()
I deleted the first argument to let matplotlib automatically choose logical and readable spacing of the axis labels.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  create new column based on condition arvin 12 2,227 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  Python create a spreadsheet with column and row header ouruslife 4 1,615 Jul-09-2022, 11:01 AM
Last Post: Pedroski55
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,503 Jan-20-2022, 05:15 PM
Last Post: deanhystad
  Create zip file from the BLOB column in ORACLE DB nnsatpute 2 1,929 Dec-31-2021, 11:00 AM
Last Post: ibreeden
  Solving periodic cot(x) function TheGoodTheBadTheUgly 1 1,597 Sep-26-2021, 06:07 AM
Last Post: ndc85430
  periodic boundary contions grknkilicaslan 1 1,898 Jul-18-2020, 10:16 PM
Last Post: Gribouillis
  Create new column in new created table farhana88 1 1,815 Jun-09-2020, 07:20 AM
Last Post: buran
  Substring and If then Condition to create column Chandan 2 2,354 Jan-23-2020, 08:40 AM
Last Post: buran
  Create a monthly mean column in multiple years fyec 1 4,017 Jun-21-2018, 03:57 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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