尝试将.insert()插入数据框时出错

时间:2019-07-10 17:52:11

标签: python pandas dataframe

我有一些使用csv文件的代码,该文件每天查找最小/最大,然后告诉我发生什么时间。我也有2个变量来查找最大/最小百分比。

这是当前数据框的输出

>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
>

然后我有2个变量表示高/低的百分比。(仅显示了ph)

>[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
>

我尝试执行.insert(),但收到此错误。

TypeError:insert()接受4到5个位置参数,但给出了6个

这是我的代码

#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()

我希望输出在第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

>

1 个答案:

答案 0 :(得分:1)

您每次只能通过插入添加一列。而且,当您打算在数据框的末尾添加新列时,甚至不需要插入:

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

如果您坚持使用插入,则正确的语法应为:

result.insert(2, "High %", ph)
result.insert(3, "Low %", pl)