时间序列操纵

时间:2015-12-30 14:27:13

标签: python-2.7 pandas time-series

所以我有一个数据框,我将时间序列转储到。 索引是日期。我需要根据日期进行计算。

例如。我有{

     XRT_Close
Date                 
2010-01-04      35.94
2010-01-05      36.17
2010-01-06      36.50
...
2015-02-07      36.60
2015-02-08      36.52 }

我该怎么说呢...从月末到月底的百分比变化?我将如何构建循环以循环几个月?

任何帮助都将获得巨大的赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

首先创建年份和月份列:

function addRow(tableID) {
        // find the number of rows in the table myreservations

        <% for(var i = 0;i<reservations.length;i++) { %>

            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;
            var foundalready = false;
            var row = table.insertRow(rowCount);

            for(var i=0; i<rowCount;i++){

                if(<%- JSON.stringify(reservations[i].rid)%> == table.rows[i].cells[6].id){

                    foundalready = true;
                    break;

                }
            }

            if (foundalready == false){    
                var cell1 = row.insertCell(0);
                var element1 = document.createElement("input");
                element1.type = "checkbox";
                element1.name = "chkbox[]";
                cell1.appendChild(element1);

                var cell2 = row.insertCell(1);
                cell2.innerHTML = rowCount;

                var cell3 = row.insertCell(2);
                var element2 = document.createElement("div");
                element2.type = "text";
                element2.textContent=<%- JSON.stringify(reservations[i].tablenumber); %>;
                element2.name = "txtbox[]";
                cell3.appendChild(element2);

                var cell4 = row.insertCell(3);
                var element3 = document.createElement("div");
                element3.type = "text";
                element3.textContent=<%- JSON.stringify(reservations[i].stime); %>;
                element3.name = "txtbox[]";
                cell4.appendChild(element3);

                var cell5 = row.insertCell(4);
                var element4 = document.createElement("div");
                element4.type = "text";
                element4.textContent=<%- JSON.stringify(reservations[i].etime); %>;
                element4.name = "txtbox[]";
                cell5.appendChild(element4);

                var cell6 = row.insertCell(5);
                var element5 = document.createElement("div");
                element5.type = "text";
                element5.textContent=<%- JSON.stringify(reservations[i].priority); %>;
                element5.name = "txtbox[]";
                cell6.appendChild(element5);

                var cell7 = row.insertCell(6);
                var element6 = document.createElement("div");
                element6.type = "div";
                element6.id=<%- JSON.stringify(reservations[i].rid); %>;
                element6.name = "txtbox[]";
                element6.textContent = <%- JSON.stringify(reservations[i].rid); %>;
                cell7.appendChild(element6);
            }
                <% } %>

            }

由他们分组:

df['year'] = [x.year for x in df.index]
df['month'] = [x.month for x in df.index]

定义要在群组上运行的功能:

grouped = df.groupby(['year','month'])

将该功能应用于群组:

def PChange(df):
    begin = df['column_name'].iloc[0]
    end = df['column_name'].iloc[-1]
    return (end-begin)/(end+begin)*100

让我知道它是否有效。