滑动窗口与Pandas DataFrame重叠50%

时间:2017-08-01 18:24:18

标签: pandas dataframe sliding-window

说我有这样的数据框:

              x   y   z
timestamp
some_date_1   5   2   4
some_date_2   1   2   6
some_date_3   7   3   5
 ...
some_date_50  4   3   6

我希望应用一个大小为10的滑动窗口(称之为变量window_size),重叠率为50%(使其成为变量step_size,这是window_size的一半) xyz列。def sliding_window(df, window_size, step_size): timestamp x y z timestamp some_date_1 5 2 4 some_date_2 1 2 6 some_date_3 2 3 1 some_date_4 5 4 4 x y z timestamp some_date_3 2 3 1 some_date_4 5 4 4 some_date_5 6 7 9 some_date_6 2 1 8 列。因此,我会打印0到9的前10行。之后,我会打印5 - 14,10 - 19,15 - 24等。

如果我有一个功能,我该怎么做?

<script>
  $(document).ready(function () {

    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('.scrollup').fadeIn();
        } else {
            $('.scrollup').fadeOut();
        }
    });

        $('.scrollup').click(function () {
          $("html, body").animate({
            scrollTop: 0
          }, 600, function() {
             var baseurl = window.location.origin+window.location.pathname;
             $(location).delay(800).attr('href', baseurl + this.getAttribute('href'));
          });
       return false;
    });
  });
</script>

假设<!-- Sticky Navbar --> <nav class="navbar" role="navigation"> <!-- Toggle Button --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-ex1-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar top-bar"></span> <span class="icon-bar middle-bar"></span> <span class="icon-bar bottom-bar"></span> </button> </div> <!-- /Toggle Button --> <!-- Center Menu --> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav navbar-top navbar-left"> <!-- <li class="active"> <a class="animated-menu-item" href="#">Home</a> </li> --> <li> <a class="animated-menu-item scrollup" href="/our-wines">OUR WINES</a> </li> <li> <a class="animated-menu-item" href="/about-us">ABOUT US</a> </li> </ul> <ul class="nav navbar-nav navbar-top navbar-right"> <li> <a class="animated-menu-item" href="/latest">LATEST</a> </li> <li> <a class="animated-menu-item" href="/contact">CONTACT</a> </li> </ul> </div> <!-- /Center Menu --> </nav> 是日期时间。

我希望每个窗口都有单独的结构。因此,例如,我想为前十行创建一个单独的DataFrame,然后为下一个十行创建另一个DataFrame等。

为简单起见,我将展示窗口大小为4且步长为2的示例。

public class landingPage extends AppCompatActivity {
private EditText username;
EditText passwordSave;
static final int READ_BLOCK_SIZE = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_landing);

    passwordSave = (EditText) findViewById(R.id.password);
}


public void submitPassword(View view) {
    if (passwordSave.equals("")) {
        Snackbar snackbar = Snackbar
                .make(view, "Please fill in your password", Snackbar.LENGTH_LONG)
                .setAction("Close", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                    }
                });

        snackbar.setActionTextColor(Color.RED);


        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.WHITE);
        snackbar.show();
    } else {
    Intent addEntryIntent = new Intent(this, homePage.class);
    startActivity(addEntryIntent);

    finish();
}}

1 个答案:

答案 0 :(得分:2)

考虑数据框df

df = pd.DataFrame(np.arange(1, 73).reshape(-1, 3), columns=list('xyz'))
df
def windows(d, w, t):
    r = np.arange(len(d))
    s = r[::t]
    z = list(zip(s, s + w))
    f = '{0[0]}:{0[1]}'.format
    g = lambda t: d.iloc[t[0]:t[1]]
    return pd.concat(map(g, z), keys=map(f, z))

这会返回一个pd.MultiIndex的数据框,我们可以使用loc轻松访问每个窗口

wdf = windows(df, 10, 5)

wdf.loc['0:10']

    x   y   z
0   1   2   3
1   4   5   6
2   7   8   9
3  10  11  12
4  13  14  15
5  16  17  18
6  19  20  21
7  22  23  24
8  25  26  27
9  28  29  30

或者

wdf.loc['15:25']

     x   y   z
15  46  47  48
16  49  50  51
17  52  53  54
18  55  56  57
19  58  59  60
20  61  62  63
21  64  65  66
22  67  68  69
23  70  71  72