获得以下DataFrame:
Body Weight (KG) Exercise 1 Weight (KG) Exercise 2 \
Date
17/07/17 77.9 Squat 20.0 Bench Press
Weight (KG).1 Exercise 3 Weight (KG).2
Date
17/07/17 20.0 Barbell Row 30.0
尝试重塑整洁的格式:
Date Body Weight Exercise Weight
17/07/17 77.9 Squat 20.0
17/07/17 77.9 Bench Press 20.0
17/07/17 77.9 Barbell Row 30.0
尝试使用融合功能
pd.melt(df,value_vars=["Exercise 1","Exercise 2","Exercise 3"])
导致:
variable value
0 Exercise 1 Squat
1 Exercise 2 Bench Press
2 Exercise 3 Barbell Row
如何让权重和其他列“排列”正确的日期和练习?
答案 0 :(得分:1)
您可以先Body Weight (KG)
将MultiIndex
列添加到DataFrame
,然后通过重复index
值来调用Notice
构造函数。
Exercise
- 首先需要在所有列中配对Weight
和df = df.set_index('Body Weight (KG)', append=True)
a = len(df.index)
b = int(len(df.columns) / 2)
df = pd.DataFrame(df.values.reshape(a * b ,2),
index=df.index.repeat(b),
columns=['Exercise', 'Weight']).reset_index()
print (df)
Date Body Weight (KG) Exercise Weight
0 17/07/17 77.9 Squat 20
1 17/07/17 77.9 Bench Press 20
2 17/07/17 77.9 Barbell Row 30
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_IMAGE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().replace(R.id.frame, new Image_Selecter()).commit();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Utilz.printLog("Parentactivity", "onActivityResult");
Log.e("hererererer", "hererererer");
if (requestCode == REQUEST_IMAGE) {
new Image().onActivityResult(requestCode, resultCode, data);
}
}
}