如下所示的两个数据帧,我想计算相关系数。
当两列都用实际值完成时,它可以正常工作。但是,如果不是,则在计算相关系数时取零作为值。
例如,Addison和Caden的权重为0。Jack和Noah没有权重。我想排除它们进行计算。
(在尝试中,似乎只考虑了相同的长度,即杰克和诺亚自动被排除在外-是吗?)
如何只包含非零值的人员进行计算?
谢谢。
import pandas as pd
Weight = {'Name': ["Abigail","Addison","Aiden","Amelia","Aria","Ava","Caden","Charlotte","Chloe","Elijah"],
'Weight': [10, 0, 12, 20, 25, 10, 0, 18, 16, 13]}
df_wt = pd.DataFrame(Weight)
Score = {'Name': ["Abigail","Addison","Aiden","Amelia","Aria","Ava","Caden","Charlotte","Chloe","Elijah", "Jack", "Noah"],
'Score': [360, 476, 345, 601, 604, 313, 539, 531, 507, 473, 450, 470]}
df_sc = pd.DataFrame(Score)
print df_wt.Weight.corr(df_sc.Score)
答案 0 :(得分:2)
屏蔽并采用非零值和公共索引:
mask1 = df_wt['Weight'].ne(0)
mask2 = df_sc['Score'].ne(0)
common_index = df_wt.loc[mask1, :].index.intersection(df_sc.loc[mask2, :].index)
df_wt.loc[common_index, 'Weight'].corr(df_sc.loc[common_index, 'Score'])
如果两个数据帧均包含零,则:
@Override
public void onDisplayVideo(String url) {
ivImageAds.setVisibility(View.GONE);
surfaceView.setVisibility(View.VISIBLE);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setDataSource(url);
mediaPlayer.setOnCompletionListener(mp -> {
// release it here after using.
releaseMediaPlayer();
adsViewPresenter.mediaPlayed();
});
mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
if (Build.VERSION.SDK_INT >= 21) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mediaPlayer.prepareAsync();
} catch (IOException e) {
adsViewPresenter.mediaError(e);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceDestroyed(SurfaceHolder holder) { }
});
}
private void releaseMediaPlayer() {
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
@Override
protected void onDestroy() {
super.onDestroy();
releaseMediaPlayer();
}
答案 1 :(得分:1)
使用map
添加新列,删除boolean indexing
的0
行,最后在同一DataFrame中应用您的解决方案:
df_wt['Score'] = df_wt['Name'].map(df_sc.set_index('Name')['Score'])
df_wt = df_wt[df_wt['Weight'].ne(0)]
print (df_wt)
Name Weight Score
0 Abigail 10 360
2 Aiden 12 345
3 Amelia 20 601
4 Aria 25 604
5 Ava 10 313
7 Charlotte 18 531
8 Chloe 16 507
9 Elijah 13 473
print (df_wt.Weight.corr(df_wt.Score))
0.923425144491911