我按照chrologically对DataFrame进行了排序,让我们举个例子:
import pandas as pd
import numpy as np
dates = pd.date_range('2011-04-01 00:00', periods=300, freq='min')
random_dates = pd.to_datetime(np.random.choice(dates, size=20,replace=False)).sort_values()
numbers = np.random.uniform(low=-1, high=1, size=(20,))
df = pd.DataFrame(index=random_dates, data=numbers)
如果我们打印它:
...
2011-04-01 02:03:00 -0.404476
2011-04-01 02:38:00 0.205260
2011-04-01 02:44:00 0.111812
2011-04-01 03:10:00 -0.071028
2011-04-01 03:55:00 -0.203999
如何获取属于DataFrame timeindex的最后N分钟/小时的行?
因此,例如,如果我想要最后一小时(从最后一行开始计算),我将从上面的示例中获取最后两行。
答案 0 :(得分:3)
假设您的数据框按索引排序,您可以使用import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int number_of_rotors;
//Views as member variables so that kept in memory until activity destroys.
EditText Battery_Capacity,Amps_perMotor,Thrust_perMotor,Weight_ofDrone;
Spinner typeOfDrone;
Button CalculateFTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Battery_Capacity = (EditText) findViewById(R.id.batteryCapacity);
Amps_perMotor = (EditText) findViewById(R.id.MAPM);
Thrust_perMotor = (EditText) findViewById(R.id.MTPM);
Weight_ofDrone = (EditText) findViewById(R.id.droneWeight);
typeOfDrone = (Spinner) findViewById(R.id.droneType);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.types));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeOfDrone.setAdapter(myAdapter);
typeOfDrone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String drone_type = typeOfDrone.getSelectedItem().toString();
switch (drone_type){
case "Tricopter":
number_of_rotors = 3;
break;
case "Quadcopter":
number_of_rotors = 4;
break;
case "Hexacopter":
number_of_rotors = 6;
break;
case "Octacopter":
number_of_rotors = 8;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
// typeOfDrone.setSelection(1);
//public void calculateFT()
CalculateFTime = (Button) findViewById(R.id.calculate_flight_time);
CalculateFTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Values from editTexts should be collected at the time of button click event.
String stringA = Battery_Capacity.getText().toString();
String stringB = Amps_perMotor.getText().toString();
String stringC = Thrust_perMotor.getText().toString();
String stringD = Weight_ofDrone.getText().toString();
// Removed null checks on strings as EditText never returns null as per android docs
//but empty checks are necessary to avoid app crash in case of parsing exception of empty string
int battery_capacity_mah =(Integer.parseInt(!stringA.isEmpty()?stringA:"0")) * (80/100);
int total_thrust = Integer.parseInt(!stringC.isEmpty()?stringC:"0") * number_of_rotors;//no usage found, so did not confirm it
int total_max_ampdraw = Integer.parseInt(stringB.isEmpty()||stringB.equals("0")?"1":stringB) * number_of_rotors;//Simplified
float kgthrust = total_thrust/1000;
float required_throttle = (Float.parseFloat(stringD.isEmpty()||stringD.equals("0")?"1":stringD)/1000) / kgthrust;
float avg_ampdraw=required_throttle*total_max_ampdraw;
String abc = Float.toString(avg_ampdraw);
Toast.makeText(MainActivity.this,abc,Toast.LENGTH_SHORT);
float battery_capacity_ah=battery_capacity_mah/1000;
float time_in_hours=battery_capacity_ah/avg_ampdraw;
float time_in_minutes=time_in_hours*60;
float time_in_seconds=time_in_minutes*60;
int tis = (int)Math.round(time_in_seconds);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Calculated Flight Time")
.setMessage("Your UAV will fly for "+tis+" seconds, which is equivalent to "+time_in_minutes+" minutes.")
.setNeutralButton("OK",null)
.show();
}
});
}
}
从最终索引条目中减去任意时间量。
然后根据pd.Timedelta
过滤您的数据框。
df.index