如何根据新职位列表对列进行重新排序

时间:2018-08-10 08:21:07

标签: python pandas

我有一个尺寸为(7096,94)的熊猫数据框,每个索引都有一个名称。 我想用该位置/位置列表重新排列列:

   public class AudioPlaybackListener implements PlaybackListener, AudioManager.OnAudioFocusChangeListener,
    ExoPlayer.EventListener{

private static final String TAG = Logger.makeLogTag(com.business.syndriabs.audiobookplayer.audiostreamer.AudioPlaybackListener.class);
public static final float VOLUME_DUCK = 0.2f;
public static final float VOLUME_NORMAL = 1.0f;
private static final int AUDIO_NO_FOCUS_NO_DUCK = 0;
private static final int AUDIO_NO_FOCUS_CAN_DUCK = 1;
private static final int AUDIO_FOCUSED = 2;
private final Context mContext;
private final WifiManager.WifiLock mWifiLock;
private int mState;
private PlaybackListener.Callback mCallback;
private boolean mPlayOnFocusGain;
private volatile int mCurrentPosition;
private volatile String mCurrentMediaId;
private int mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
private final AudioManager mAudioManager;
private SimpleExoPlayer simpleExoPlayer;
private boolean isPlaying = false;

private BandwidthMeter bandwidthMeter;
private ExtractorsFactory extractorsFactory;
private TrackSelection.Factory trackSelectionFactory;
private TrackSelector trackSelector;
private DefaultBandwidthMeter defaultBandwidthMeter;
private DataSource.Factory dataSourceFactory;
private MediaSource mediaSource;
private String source;

public AudioPlaybackListener(Context context) {
    this.mContext = context;
    this.mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    this.mWifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "dmAudioStreaming_Lock");
    this.mState = simpleExoPlayer.STATE_IDLE;
}

private final IntentFilter mAudioNoisyIntentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private final BroadcastReceiver mAudioNoisyReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
            Logger.d(TAG, "Headphones disconnected.");
            if (isPlaying()) {
                //TODO PAUSE THE PLAYING SONG
            }
        }
    }
};

@Override
public void start() {
}

@Override
public void stop(boolean notifyListeners) {
    mState = PlaybackStateCompat.STATE_STOPPED;
    if (notifyListeners && mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
    mCurrentPosition = getCurrentStreamPosition();
    giveUpAudioFocus();
    unregisterAudioNoisyReceiver();
    relaxResources(true);
}


@Override
public void setState(int state) {
    this.mState = state;
}

@Override
public int getState() {
    return mState;
}

@Override
public boolean isConnected() {
    return true;
}

@Override
public boolean isPlaying() {
    return mPlayOnFocusGain || (simpleExoPlayer != null && simpleExoPlayer.getPlayWhenReady());
}

@Override
public boolean isStop() {
    return mPlayOnFocusGain || (simpleExoPlayer != null && !simpleExoPlayer.getPlayWhenReady());
}


@Override
public int getCurrentStreamPosition() {
    return simpleExoPlayer != null ? (int) simpleExoPlayer.getCurrentPosition() : mCurrentPosition;
    // return mMediaPlayer != null ? mMediaPlayer.getCurrentWindowIndex() : mCurrentPosition;
}

@Override
public void updateLastKnownStreamPosition() {
    if (simpleExoPlayer != null) {
        mCurrentPosition = (int) simpleExoPlayer.getCurrentPosition();
    }
}

@Override
public void play(MediaMetaData item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }
    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && simpleExoPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        source = item.getMediaUrl();
        if (source != null) {
            source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
        }

        createMediaPlayerIfNeeded();
        bandwidthMeter = new DefaultBandwidthMeter();
        extractorsFactory = new DefaultExtractorsFactory();

        trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

        trackSelector = new DefaultTrackSelector(trackSelectionFactory);

        defaultBandwidthMeter = new DefaultBandwidthMeter();
        dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, "mediaPlayerSample"), defaultBandwidthMeter);


        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
        mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
        mState = PlaybackStateCompat.STATE_BUFFERING;
        simpleExoPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        simpleExoPlayer.prepare(mediaSource);
        simpleExoPlayer.setPlayWhenReady(true);
        mWifiLock.acquire();

        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }

    }
}

@Override
public void pause() {
    try {
        if (isPlaying()) {
            // Pause media player and cancel the 'foreground service' state.
            if (simpleExoPlayer != null && simpleExoPlayer.getPlayWhenReady()) {
                simpleExoPlayer.setPlayWhenReady(false);
                mCurrentPosition = (int) simpleExoPlayer.getCurrentPosition();
            }
            // while paused, retain the MediaPlayer but give up audio focus
            relaxResources(false);
        }
        mState = PlaybackStateCompat.STATE_PAUSED;
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        unregisterAudioNoisyReceiver();
    } catch (IllegalStateException ex) {
        Logger.e(TAG, ex, "Exception pause IllegalStateException");
        ex.printStackTrace();
    }
}


@Override
public void seekTo(int position) {
    Logger.d(TAG, "seekTo called with ", position);

    if (simpleExoPlayer == null) {
        // If we do not have a current media player, simply update the current position
        mCurrentPosition = position;
    } else {
        if (isPlaying()) {
            mState = PlaybackStateCompat.STATE_BUFFERING;
        }
        registerAudioNoisyReceiver();
        simpleExoPlayer.seekTo(position);
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
    }
}

@Override
public void setCallback(Callback callback) {
    this.mCallback = callback;
}

@Override
public void setCurrentStreamPosition(int pos) {
    this.mCurrentPosition = pos;
}

@Override
public void setCurrentMediaId(String mediaId) {
    this.mCurrentMediaId = mediaId;
}

@Override
public String getCurrentMediaId() {
    return mCurrentMediaId;
}

private void tryToGetAudioFocus() {
    Logger.d(TAG, "tryToGetAudioFocus");
    int result = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_FOCUSED;
    } else {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

private void giveUpAudioFocus() {
    Logger.d(TAG, "giveUpAudioFocus");
    if (mAudioManager.abandonAudioFocus(this) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        mAudioFocus = AUDIO_NO_FOCUS_NO_DUCK;
    }
}

private void configMediaPlayerState() {
    Logger.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
    if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
        // If we don't have audio focus and can't duck, we have to pause,
        if (isPlaying()) {
            mPlayOnFocusGain = false;
            pause();
        }
    } else {  // we have audio focus:
        registerAudioNoisyReceiver();
        if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
            simpleExoPlayer.setVolume(VOLUME_DUCK); // we'll be relatively quiet
        } else {
            if (simpleExoPlayer != null) {
                simpleExoPlayer.setVolume(VOLUME_NORMAL); // we can be loud again
            } 
        }
        if (mPlayOnFocusGain) {
            if (simpleExoPlayer != null && !isPlaying) {
                Logger.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ",
                        mCurrentPosition);
                if (mCurrentPosition == simpleExoPlayer.getCurrentPosition()) {
                    simpleExoPlayer.setPlayWhenReady(true);
                    isPlaying();
                } else {
                    simpleExoPlayer.seekTo(mCurrentPosition);
                    mState = PlaybackStateCompat.STATE_BUFFERING;
                }
            }
            mPlayOnFocusGain = false;
        }
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

@Override
public void onAudioFocusChange(int focusChange) {
    Logger.d(TAG, "onAudioFocusChange. focusChange=", focusChange);
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } else if (focusChange == AudioManager.AUDIOFOCUS_LOSS ||
            focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
            focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {

        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        if (isPlaying() && !canDuck) {
            mPlayOnFocusGain = true;
        }
    } else {
        Logger.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

private void createMediaPlayerIfNeeded() {
    Logger.d(TAG, "createMediaPlayerIfNeeded. needed? ", (simpleExoPlayer == null));
    if (simpleExoPlayer == null) {
        bandwidthMeter = new DefaultBandwidthMeter();
        extractorsFactory = new DefaultExtractorsFactory();

        trackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

        trackSelector = new DefaultTrackSelector(trackSelectionFactory);

        defaultBandwidthMeter = new DefaultBandwidthMeter();
        dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, "mediaPlayerSample"), defaultBandwidthMeter);

        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
        simpleExoPlayer.addListener(this);

    } else {
        simpleExoPlayer.stop();
        simpleExoPlayer.setPlayWhenReady(true);
    }
}

private void relaxResources(boolean releaseMediaPlayer) {
    Logger.d(TAG, "relaxResources. releaseMediaPlayer=", releaseMediaPlayer);

    if (releaseMediaPlayer && simpleExoPlayer != null) {
        simpleExoPlayer.stop();
        simpleExoPlayer.setPlayWhenReady(true);
        simpleExoPlayer.release();
        simpleExoPlayer = null;
        mCurrentMediaId = null;
        mCurrentPosition = 0;
    }

    if (mWifiLock.isHeld()) {
        mWifiLock.release();
    }
}

private void registerAudioNoisyReceiver() {
    try {
        if (mAudioNoisyReceiver!=null) {
            mContext.registerReceiver(mAudioNoisyReceiver, mAudioNoisyIntentFilter);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void unregisterAudioNoisyReceiver() {
    try {
        if (mAudioNoisyReceiver!=null) {
            mContext.unregisterReceiver(mAudioNoisyReceiver);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {

}

@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {

}

@Override
public void onLoadingChanged(boolean isLoading) {

}

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

    switch(playbackState) {
        case ExoPlayer.STATE_BUFFERING:
            break;
        case ExoPlayer.STATE_ENDED:
            simpleExoPlayer.stop();
            break;
        case ExoPlayer.STATE_IDLE:
            break;
        case ExoPlayer.STATE_READY:
            configMediaPlayerState();
            break;
        default:
            break;
    }

}

@Override
public void onRepeatModeChanged(int repeatMode) {

}

@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {

}

@Override
public void onPlayerError(ExoPlaybackException error) {
    simpleExoPlayer.stop();

    Logger.e(TAG, "Media player error: what=" + error );
    if (mCallback != null) {
        mCallback.onError("MediaPlayer error " + error );
    }
}

@Override
public void onPositionDiscontinuity(int reason) {

}

@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {

}

@Override
public void onSeekProcessed() {

    Logger.d(TAG, "onSeekComplete from MediaPlayer:", simpleExoPlayer.getCurrentPosition());
    mCurrentPosition = (int) simpleExoPlayer.getCurrentPosition();
    if (mState == PlaybackStateCompat.STATE_BUFFERING) {
        registerAudioNoisyReceiver();
        simpleExoPlayer.setPlayWhenReady(true);
        isPlaying();
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}
}

51、7、36、23、15、59、71、0、5、91、39、67、90、77、86、2、61、69、82、46、47、45、85、75 ,  83、6、88、65、34、52、4、8、29、38、35、33、60、84、80、49、24、13、3、14、12、68、16、17  41,31,10,87,32,11,20,76,43,66,48,37,44,74,42,22,58,1,19,50,30,9]

2 个答案:

答案 0 :(得分:2)

cols = [92, 57, 73, 81, 62, 64, 18, 93, 63, 89, 56, 72, 78, 26, 28, 79, 40, 54, 55, 53, 27, 21, 70, 25, 51, 7, 36, 23, 15, 59, 71, 0, 5, 91, 39, 67, 90, 77, 86, 2, 61, 69, 82, 46, 47, 45, 85, 75, 83, 6, 88, 65, 34, 52, 4, 8, 29, 38, 35, 33, 60, 84, 80, 49, 24, 13, 3, 14, 12, 68, 16, 17, 41, 31, 10, 87, 32, 11, 20, 76, 43, 66, 48, 37, 44, 74, 42, 22, 58, 1, 19, 50, 30, 9]

cols = [df.columns[c] for c in cols]
df = df[cols]

答案 1 :(得分:1)

您可以使用pd.DataFrame.iloc直接输入列索引:

cols = [92, 57, 73, 81, 62, 64, 18, ...]
df = df.iloc[:, cols]