0

I have some code that takes a csv file, that finds the min/max each day, then tells me what time that happens. I also have 2 variables to find the percentage for both max/min.

This is currently the output for the dataframe

>Out
       High   Low
10:00   6.0  10.0
10:05  10.0   3.0
10:10   1.0   7.0
10:15   1.0   NaN
10:20   4.0   4.0
10:25   4.0   4.0
10:30   5.0   1.0
10:35   5.0   6.0
10:40   3.0   2.0
10:45   4.0   5.0
10:50   4.0   1.0
10:55   3.0   4.0
11:00   4.0   5.0
>

Then I have 2 varables for the % of High/Lows.. (Just ph shown)

>[84 rows x 2 columns]
Time
10:00    0.015306
10:05    0.025510
10:10    0.002551
10:15    0.002551
10:20    0.010204
10:25    0.010204
>

I've tried to do an .insert(), but recieve this error.

TypeError: insert() takes from 4 to 5 positional arguments but 6 were given

This was my code

#adding % to end of dataframe
result.insert(3,"High %", ph, "Low %", pl)

import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv("C:\\Users\\me\\Downloads\\file.csv", encoding = "ISO-8859-1")


#High grouped by Date
df2 = df.loc[df.groupby('Date')['High'].idxmax()]


#dropping columns of no use
df2.drop(['Ticker','Open','Low','Close'], axis=1, inplace=True)

#creating a variable to bucket the time
TH = df2.groupby('Time').size()

#Low grouped by Date
df3 = df.loc[df.groupby('Date')['Low'].idxmin()]

#dropping columns of no use
df3.drop(['Ticker','Open','Low','Close'], axis=1, inplace=True)

#creating a variable to bucket the time
TL = df3.groupby('Time').size()

#Merging Both Dataframes
frames = [TH, TL]
result = pd.concat((frames), axis = 1)
result.columns = ['High','Low']

#Percentage
ph = TH/TH.sum()
pl = TL/TL.sum()

I would like the output to show the % in columns 3 &4

>Out
       High   Low    % High    %Low
10:00   6.0  10.0    .015306   
10:05  10.0   3.0    .025510
10:10   1.0   7.0    .002551
10:15   1.0   NaN    .002551
10:20   4.0   4.0    .010204
10:25   4.0   4.0    .010204

>
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    Please see [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and edit your sample input and output to make it a little easier to work with – G. Anderson Jul 10 '19 at 18:03

1 Answers1

1

You can only add one column at a time with insert. And as you intend to add the new columns at the end of the dataframe you do not even need insert:

#adding % to end of dataframe
result["High %"] = ph
result["Low %"] = pl

If you insist on using insert the correct syntax would be:

result.insert(2, "High %", ph)
result.insert(3, "Low %", pl)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252