如何解决为什么我在pandas数据框中找出一个变量与所有其他变量的相关性时出错
考虑以下数据集(作为 pandas df 导入):
import android.content.Context;
import android.widget.FrameLayout;
import com.google.android.exoplayer2.Player.EventListener;
import androidx.annotation.NonNull;
public class VideoPlayerView extends FrameLayout {
private Context context;
public VideoPlayer videoPlayer;
public VideoPlayerView(@NonNull Context context) {
super(context);
this.context = context;
this.init();
}
private void init(){
videoPlayer = new VideoPlayer(context);
videoPlayer.addOnAttachStateChangeListener(new EventListener() {
@Override
public void onPlaybackStateChanged(int state) {
post(measureAndLayout);
}
@Override
public void onIsPlayingChanged(boolean isPlaying) {
post(measureAndLayout);
}
});
this.addView(videoPlayer);
}
private final Runnable measureAndLayout = () -> {
measure(
MeasureSpec.makeMeasureSpec(getWidth(),MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(getHeight(),MeasureSpec.EXACTLY));
layout(getLeft(),getTop(),getRight(),getBottom());
};
}
我想要做的是找出 Chinese Yuan Euro Japanese Yen U.K. Pound Sterling U.S. Dollar Indian Rupee
9.29446 1.25367 143.645 1.05054 1.39596 94.1451
9.31515 1.25322 142.715 1.06143 1.39684 94.1513
9.31697 1.25834 140.54 1.073 1.39286 NA
9.31315 1.25737 140.64 1.06911 1.39316 94.0345
9.3135 1.25797 140.51 1.07261 1.39257 93.9408
9.29403 1.25769 139.962 1.0705 1.38962 93.3027
9.31021 1.2549 143.369 1.05762 1.39194 93.4641
9.3135 1.25716 145.178 1.0468 1.39193 93.5445
9.30432 1.24695 144.895 1.05236 1.39122 93.0917
9.31532 1.25263 147.268 1.04242 1.39392 NA
9.30354 1.25652 NA 1.04952 1.38883 93.1923
和所有其他货币的相关性,为此,我尝试使用以下 python 代码:
Indian Rupee
上面抛出了一个错误:
df['Indian Rupee'].corr(~df['Indian Rupee'])
我想了解为什么会出现上述错误?为什么我无法通过这种方式找到相关性?
我在这里有什么选择?
解决方法
一种选择是对整个数据框进行关联,然后选择您关心的列
df.corr()['Indian Rupee']
ChineseYuan 0.304050
Euro 0.243851
JapaneseYen -0.270123
U.K.PoundSterling 0.314681
U.S.Dollar 0.872862
IndianRupee 1.000000
Name: IndianRupee,dtype: float64
,
~
是按位取反运算符。您可以否定布尔值(系列)或整数(系列)。您不能否定浮动系列,这是不安全。
换句话说,~df['Indian Rupee']
不会为您排除其他列。如果要删除,请使用:
df.drop('Indian Rupee',axis=1)
所以你可以这样做
df.drop('Indian Rupee',axis=1).corrwith(df['Indian Rupee'])
输出:
Chinese Yuan 0.267802
Japanese Yen -0.270123
U.K. Pound Sterling 0.197496
U.S. Dollar 0.846584
dtype: float64
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。