熊猫掉落级别并附加到列标题

时间:2018-06-21 18:48:56

标签: pandas

使用枢轴功能,我设法获得了扁平数据框:

q_id                                     1              2      
a_id                                  1  2  3  4    5  6  7  8
movie_id     user_id    start_rating                           
931       284                 2.0     0  0  0  1    0  0  0  0   
804       648                 4.5     0  0  0  0    1  0  0  0   
840       414                 4.5     0  1  0  0    0  0  0  0   
843       419                 3.5     1  0  0  0    0  1  0  0   
848       132                 3.5     1  0  0  1    0  0  0  0 

我的目标是删除索引和列名的附加级别。

movie_id     user_id    start_rating  1_1  1_2  1_3  1_4  2_5  2_6  2_7  2_8                         
931       284                 2.0     0  0  0  1    0  0  0  0   
804       648                 4.5     0  0  0  0    1  0  0  0   
840       414                 4.5     0  1  0  0    0  0  0  0   
843       419                 3.5     1  0  0  0    0  1  0  0   
848       132                 3.5     1  0  0  1    0  0  0  0 

我尝试了以下步骤:

df.columns = ['_'.join(col).strip() for col in df.columns.values]

但得到:

    df.columns = ['_'.join(col).strip() for col in df.columns.values]
TypeError: sequence item 0: expected string, int found

1 个答案:

答案 0 :(得分:1)

函数join适用于字符串,错误显示col的元素为 SpeechToText speechToText = new SpeechToText(); speechToText.setEndPoint("https://stream.watsonplatform.net/speech-to-text/api/v1/recognize"); speechToText.setUsernameAndPassword("{myUsername}", "{myPassword}"); try { RecognizeOptions recognizeOptions = new RecognizeOptions.Builder() .audio(new FileInputStream("KATL-App-Final-All-Aug-01-2017-1630.mp3-edited.mp3")) .contentType("audio/mp3") .model("en-US_NarrowbandModel") .interimResults(true) .build(); BaseRecognizeCallback baseRecognizeCallback = new BaseRecognizeCallback() { @Override public void onListening() { System.out.println("Listening"); } @Override public void onConnected() { System.out.println("Connected"); } @Override public void onTranscription(SpeechRecognitionResults speechRecognitionResults) { System.out.println(speechRecognitionResults); } @Override public void onDisconnected() { System.exit(0); } }; speechToText.recognizeUsingWebSocket(recognizeOptions, baseRecognizeCallback); } catch (FileNotFoundException e) { e.printStackTrace(); } for (;;) { Thread.sleep(1000); } 。您需要将int的元素转换为col

str

或者因为这里有两个级别,请执行以下操作:

df.columns = ['_'.join([str(lev) for lev in col]).strip() for col in df.columns.values]