我希望有人可以帮助我。
我有一个df,我想在“长度”列上使用replace,但是我只想在符合条件的某些行上使用replace(我已经能够做到)。问题是这些行不符合以n / a结尾的条件,但是我真的想将现有数据保留在那些行中,即不要运行replace方法。
这里是进行选择的代码,条件不包含“小时”。
mask = ~data['Length'].str.contains("hour")
这是我的替换代码,可以正常工作。我正在写原始数据。
data['Length'] = data.loc[mask, 'Length'].replace(r'([1])[h]\s', r'\1 hour ', regex=True)
data['Length'] = data.loc[mask, 'Length'].replace(r'([2-9]*)[h]\s', r'\1 hours ', regex=True)
data['Length'] = data.loc[mask, 'Length'].replace(r'([0-9]*)[m]', r'\1 minutes', regex=True)
输入数据。第0行具有我在原始数据中所需的格式。其他行需要通过替换功能运行。
0 1 hour 30 minutes
1 2h 45m
2 4h 30m
3 1h 45m
4 1h 45m
5 1h 45m
6 1h 45m
7 1h 45m
当前输出,我想将原始数据保留在第0行。
0 NaN
1 2 hours 45 minutes
2 4 hours 30 minutes
3 1 hour 45 minutes
4 1 hour 45 minutes
5 1 hour 45 minutes
6 1 hour 45 minutes
7 1 hour 45 minutes
现在,我还不习惯于使用的方法,因此,如果有更好的方法,那么我可以提出建议,但希望我缺少一些简单的方法。似乎应该很简单,因为它只是if-else。谢谢
答案 0 :(得分:0)
您还必须在作业左侧输入mask
:
# Add ".loc[mask, " on the left side:
data.loc[mask, 'Length'] = data.loc[mask, 'Length'].replace(r'([1])[h]\s', r'\1 hour ', regex=True)
data.loc[mask, 'Length'] = data.loc[mask, 'Length'].replace(r'([2-9]*)[h]\s', r'\1 hours ', regex=True)
data.loc[mask, 'Length'] = data.loc[mask, 'Length'].replace(r'([0-9]*)[m]', r'\1 minutes', regex=True)
否则,您要通过为整个data['Length']
列分配经过过滤的(较小长度的)series
来对其进行突变,这将使空白插槽自动被np.nan
填充。
答案 1 :(得分:0)
使用这些正则表达式替换然后将结果分配给// Create New Activity
package com.example.moneymanager;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class CreateNewActivity extends AppCompatActivity{
// Declare Button Variables
Button dashboardBtn, homeBtn, extrasBtn, cancelBtn, saveBtn;
EditText projectTitle, goalName;
Boolean savedFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_page);
// Link buttons to xml value
dashboardBtn = findViewById(R.id.dashboard_btn);
homeBtn = findViewById(R.id.home_btn);
extrasBtn = findViewById(R.id.extras_btn);
cancelBtn = findViewById(R.id.cancel_btn);
saveBtn = findViewById(R.id.save_btn);
// Link Edit Text with Id
projectTitle = findViewById(R.id.name_value);
goalName = findViewById(R.id.goal1_name_value);
// Set onClick listener for dashboard btn
dashboardBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
moveToDashboard();
}
});
// Set onClick listener for home btn
homeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
moveToHome();
}
});
// Set onClick listener for extras btn
extrasBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
moveToExtras();
}
});
// Listener for cancel Btn
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
moveToHome();
}
});
// Listener for save Btn
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveProject();
moveToHome();
}
});
}
/*************************Private Methods*****************************/
// Create private method for dashboard
private void moveToDashboard(){
Intent i = new Intent(this, DashboardActivity.class);
startActivity(i);
}
// Create private method for extras
private void moveToHome(){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
// Private method for extras btn
private void moveToExtras(){
Intent i = new Intent(this, ExtrasActivity.class);
startActivity(i);
}
//Private method for save Btn
private void saveProject(){
// Link user Input with defined variables
projectTitle.getText();
goalName.getText();
// Set flag to true to use in main activity
savedFlag = true;
}
}
// Main Activity
package com.example.moneymanager;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
// Declare Button Variables
Button dashboardBtn, extrasBtn;
TextView overViewText, savingsGoalText;
// Create a new instance of the CreateNewActivity Class
CreateNewActivity retrieve = new CreateNewActivity();
Boolean savedFlagIsTrue = retrieve.savedFlag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Link NAVIGATION to xml value
dashboardBtn = findViewById(R.id.dashboard_btn);
extrasBtn = findViewById(R.id.extras_btn);
// Set onClick listener for dashboard btn
dashboardBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
moveToDashboard();
}
});
// Set onClick listener for extras btn
extrasBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
moveToExtras();
}
});
// If user has saved a new project
if (savedFlagIsTrue){
String projectTitle = retrieve.projectTitle.toString();
String goalTitle = retrieve.goalName.toString();
// Link and set Overview Text with input from CreateNewActivity Class
overViewText = findViewById(R.id.overview_text);
overViewText.setText(projectTitle);
// Link and set Goal Text with input from CreateNewActivity Class
savingsGoalText = findViewById(R.id.savings_goal_text);
savingsGoalText.setText(goalTitle);
}
}
// Create private method for dashboard
private void moveToDashboard(){
Intent i = new Intent(this, DashboardActivity.class);
startActivity(i);
}
// Create private method for extras
private void moveToExtras(){
Intent i = new Intent(this, ExtrasActivity.class);
startActivity(i);
}
}
时,原始数据帧将被覆盖,并且第一行的原始值将丢失,因为会使用新的(较小的)长度为7的序列长度8系列,使用相同的索引data['Length']
(这就是第一行被NaN替换的原因)
尝试一下:
mask