融化大熊猫数据帧

时间:2017-09-03 03:57:50

标签: python pandas dataframe

我有一个像这样的数据框

    NSW     VIC
0   6718023 5023203
1   6735528 5048207
2   6742690 5061266
3   6766133 5083593
4   6786160 5103965

我想像这样改变它

0   6718023  NSW
1   6735528  NSW
2   6742690  NSW
3   6766133  NSW
4   6786160  NSW
5   5023203  VIC
6   5048207  VIC
7   5061266  VIC
8   5083593  VIC
9   5103965  VIC

我怎样才能完成这项工作?

4 个答案:

答案 0 :(得分:8)

使用melt

 pd.melt(df)
    Out[318]: 
      variable    value
    0      NSW  6718023
    1      NSW  6735528
    2      NSW  6742690
    3      NSW  6766133
    4      NSW  6786160
    5      VIC  5023203
    6      VIC  5048207
    7      VIC  5061266
    8      VIC  5083593
    9      VIC  5103965

答案 1 :(得分:4)

<script type="text/javascript">
tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4: "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: false,
        template_external_list_url: "js/template_list.js",
        external_link_list_url: "js/link_list.js",
        external_image_list_url: "js/image_list.js",
        media_external_list_url: "js/media_list.js"
    });
</script>


<td class="textboxmain" style="height:300px; "><asp:TextBox id="textbox1" TextMode="MultiLine" Height="100%" runat="server" placeholder="test............"></asp:TextBox></td>

输出:

df.stack().reset_index(level=0, drop=True).sort_index()

答案 2 :(得分:4)

使用NSW 6718023 NSW 6735528 NSW 6742690 NSW 6766133 NSW 6786160 VIC 5023203 VIC 5048207 VIC 5061266 VIC 5083593 VIC 5103965 dtype: int64

df.unstack

答案 3 :(得分:4)

FOMO

pd.DataFrame(dict(
    value=df.values.ravel(order='F'),
    variable=df.columns.repeat(len(df))
))

     value variable
0  6718023      NSW
1  6735528      NSW
2  6742690      NSW
3  6766133      NSW
4  6786160      NSW
5  5023203      VIC
6  5048207      VIC
7  5061266      VIC
8  5083593      VIC
9  5103965      VIC