當前位置: 首頁>>代碼示例>>Python>>正文


Python pandas.melt方法代碼示例

本文整理匯總了Python中pandas.melt方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.melt方法的具體用法?Python pandas.melt怎麽用?Python pandas.melt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas的用法示例。


在下文中一共展示了pandas.melt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_method_signatures

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_method_signatures(self):
        tm.assert_frame_equal(self.df.melt(),
                              melt(self.df))

        tm.assert_frame_equal(self.df.melt(id_vars=['id1', 'id2'],
                                           value_vars=['A', 'B']),
                              melt(self.df,
                                   id_vars=['id1', 'id2'],
                                   value_vars=['A', 'B']))

        tm.assert_frame_equal(self.df.melt(var_name=self.var_name,
                                           value_name=self.value_name),
                              melt(self.df,
                                   var_name=self.var_name,
                                   value_name=self.value_name))

        tm.assert_frame_equal(self.df1.melt(col_level=0),
                              melt(self.df1, col_level=0)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_melt.py

示例2: test_custom_var_name

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_custom_var_name(self):
        result5 = self.df.melt(var_name=self.var_name)
        assert result5.columns.tolist() == ['var', 'value']

        result6 = self.df.melt(id_vars=['id1'], var_name=self.var_name)
        assert result6.columns.tolist() == ['id1', 'var', 'value']

        result7 = self.df.melt(id_vars=['id1', 'id2'], var_name=self.var_name)
        assert result7.columns.tolist() == ['id1', 'id2', 'var', 'value']

        result8 = self.df.melt(id_vars=['id1', 'id2'], value_vars='A',
                               var_name=self.var_name)
        assert result8.columns.tolist() == ['id1', 'id2', 'var', 'value']

        result9 = self.df.melt(id_vars=['id1', 'id2'], value_vars=['A', 'B'],
                               var_name=self.var_name)
        expected9 = DataFrame({'id1': self.df['id1'].tolist() * 2,
                               'id2': self.df['id2'].tolist() * 2,
                               self.var_name: ['A'] * 10 + ['B'] * 10,
                               'value': (self.df['A'].tolist() +
                                         self.df['B'].tolist())},
                              columns=['id1', 'id2', self.var_name, 'value'])
        tm.assert_frame_equal(result9, expected9) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_melt.py

示例3: test_custom_value_name

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_custom_value_name(self):
        result10 = self.df.melt(value_name=self.value_name)
        assert result10.columns.tolist() == ['variable', 'val']

        result11 = self.df.melt(id_vars=['id1'], value_name=self.value_name)
        assert result11.columns.tolist() == ['id1', 'variable', 'val']

        result12 = self.df.melt(id_vars=['id1', 'id2'],
                                value_name=self.value_name)
        assert result12.columns.tolist() == ['id1', 'id2', 'variable', 'val']

        result13 = self.df.melt(id_vars=['id1', 'id2'], value_vars='A',
                                value_name=self.value_name)
        assert result13.columns.tolist() == ['id1', 'id2', 'variable', 'val']

        result14 = self.df.melt(id_vars=['id1', 'id2'], value_vars=['A', 'B'],
                                value_name=self.value_name)
        expected14 = DataFrame({'id1': self.df['id1'].tolist() * 2,
                                'id2': self.df['id2'].tolist() * 2,
                                'variable': ['A'] * 10 + ['B'] * 10,
                                self.value_name: (self.df['A'].tolist() +
                                                  self.df['B'].tolist())},
                               columns=['id1', 'id2', 'variable',
                                        self.value_name])
        tm.assert_frame_equal(result14, expected14) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:test_melt.py

示例4: test_pandas_dtypes

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_pandas_dtypes(self, col):
        # GH 15785
        df = DataFrame({'klass': range(5),
                        'col': col,
                        'attr1': [1, 0, 0, 0, 0],
                        'attr2': col})
        expected_value = pd.concat([pd.Series([1, 0, 0, 0, 0]), col],
                                   ignore_index=True)
        result = melt(df, id_vars=['klass', 'col'], var_name='attribute',
                      value_name='value')
        expected = DataFrame({0: list(range(5)) * 2,
                              1: pd.concat([col] * 2, ignore_index=True),
                              2: ['attr1'] * 5 + ['attr2'] * 5,
                              3: expected_value})
        expected.columns = ['klass', 'col', 'attribute', 'value']
        tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_melt.py

示例5: test_subclassed_melt

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_subclassed_melt(self):
        # GH 15564
        cheese = tm.SubclassedDataFrame({
            'first': ['John', 'Mary'],
            'last': ['Doe', 'Bo'],
            'height': [5.5, 6.0],
            'weight': [130, 150]})

        melted = pd.melt(cheese, id_vars=['first', 'last'])

        expected = tm.SubclassedDataFrame([
            ['John', 'Doe', 'height', 5.5],
            ['Mary', 'Bo', 'height', 6.0],
            ['John', 'Doe', 'weight', 130],
            ['Mary', 'Bo', 'weight', 150]],
            columns=['first', 'last', 'variable', 'value'])

        tm.assert_frame_equal(melted, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_subclass.py

示例6: missing_rate_plot

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def missing_rate_plot(consensus_data, ordered_genomes, biotypes, missing_plot_tgt):
    """Missing genes/transcripts"""
    base_title = 'Number of missing orthologs in consensus set'
    gene_missing_df = json_biotype_counter_to_df(consensus_data, 'Gene Missing')
    gene_missing_df.columns = ['biotype', 'Genes', 'genome']
    transcript_missing_df = json_biotype_counter_to_df(consensus_data, 'Transcript Missing')
    transcript_missing_df.columns = ['biotype', 'Transcripts', 'genome']
    df = transcript_missing_df.merge(gene_missing_df, on=['genome', 'biotype'])
    df = pd.melt(df, id_vars=['biotype', 'genome'])
    ylabel = 'Number of genes or transcripts'
    af = luigi.local_target.atomic_file(missing_plot_tgt.path)
    with PdfPages(af.tmp_path) as pdf:
        tot_df = df.groupby(['genome', 'biotype', 'variable']).aggregate(sum).reset_index()
        generic_barplot(tot_df, pdf, '', ylabel, base_title, x='genome', y='value',
                        col='variable', row_order=ordered_genomes)
        for biotype in biotypes:
            biotype_df = biotype_filter(df, biotype)
            if biotype_df is None:
                continue
            biotype_df = biotype_df.groupby(['genome', 'variable']).aggregate(sum).reset_index()
            title = base_title + ' for biotype {}'.format(biotype)
            generic_barplot(biotype_df, pdf, '', ylabel, title, x='genome', y='value',
                            col='variable', row_order=ordered_genomes)
    af.move_to_final_destination() 
開發者ID:ComparativeGenomicsToolkit,項目名稱:Comparative-Annotation-Toolkit,代碼行數:26,代碼來源:plots.py

示例7: plot_lc

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def plot_lc(lc, metrics=None, outputs=False):
    lc = pd.melt(lc, id_vars=['split', 'epoch'], var_name='output')
    if metrics:
        if not isinstance(metrics, list):
            metrics = [metrics]
        tmp = '(%s)' % ('|'.join(metrics))
        lc = lc.loc[lc.output.str.contains(tmp)]
    metrics = lc.output[~lc.output.str.contains('_')].unique()
    lc['metric'] = ''

    for metric in metrics:
        lc.loc[lc.output.str.contains(metric), 'metric'] = metric
        lc.loc[lc.output == metric, 'output'] = 'mean'
        lc.output = lc.output.str.replace('_%s' % metric, '')
        lc.output = lc.output.str.replace('cpg_', '')

    if outputs:
        lc = lc.loc[lc.output != 'mean']
    else:
        lc = lc.loc[lc.output == 'mean']

    grid = sns.FacetGrid(lc, col='split', row='metric', hue='output',
                         sharey=False, size=3, aspect=1.2, legend_out=True)
    grid.map(mpl.pyplot.plot, 'epoch', 'value', linewidth=2)
    grid.set(ylabel='')
    grid.add_legend()
    return grid 
開發者ID:cangermueller,項目名稱:deepcpg,代碼行數:29,代碼來源:dcpg_train_viz.py

示例8: format_params

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def format_params(vals, colnames, rownames,
                  embed_name, index_name='feature_id'):
    """ Reformats the model parameters in a readable format

    Parameters
    ----------
    vals : np.array
        Values of the model parameters
    colnames : array_like of str
        Column names corresponding to the features.
        These typically correspond to PC axis names.
    rownames : array_like of str
        Row names corresponding to the features.
        These typically correspond to microbe/metabolite names.
    embed_name: str
        Specifies which embedding is being formatted
    index_name : str
        Specifies the index name, since it'll be formatted
        into a qiime2 Metadata format

    Returns
    -------
    pd.DataFrame
        feature_id : str
            Feature names
        axis : str
            PC axis names
        embed_type: str
            Specifies which embedding is being formatted
        values : float
            Corresponding model parameters
    """
    df = pd.DataFrame(vals, columns=colnames, index=rownames)
    df = df.reset_index()
    df = df.rename(columns={'index': 'feature_id'})
    df = pd.melt(df, id_vars=['feature_id'],
                 var_name='axis', value_name='values')

    df['embed_type'] = embed_name

    return df[['feature_id', 'axis', 'embed_type', 'values']] 
開發者ID:biocore,項目名稱:mmvec,代碼行數:43,代碼來源:util.py

示例9: format2tidy

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def format2tidy(df, subjname, listname, subjgroup, analysis=None, position=0):
    melted_df = pd.melt(df.T)
    melted_df[subjname]=""
    for idx,sub in enumerate(melted_df['Subject'].unique()):
        melted_df.loc[melted_df['Subject']==sub,subjname]=subjgroup[idx]
    if analysis=='spc':
        base = list(df.columns)
        melted_df['Position'] = base * int(melted_df.shape[0] / len(base))
        melted_df.columns = ['Subject', listname, 'Proportion Recalled', subjname, 'Position']
    elif analysis in ['pfr', 'pnr']:
        base = list(df.columns)
        melted_df['Position'] = base * int(melted_df.shape[0] / len(base))
        melted_df.columns = ['Subject', listname, 'Probability of Recall: Position ' + str(position), subjname, 'Position']
    elif analysis=='lagcrp':
        base = list(range(int(-len(df.columns.values)/2),int(len(df.columns.values)/2)+1))
        melted_df['Position'] = base * int(melted_df.shape[0] / len(base))
        melted_df.columns = ['Subject', listname, 'Conditional Response Probability', subjname, 'Position']
    elif analysis=='fingerprint' or analysis=='fingerprint_temporal':
        base = list(df.columns.values)
        melted_df['Feature'] = base * int(melted_df.shape[0] / len(base))
        melted_df.columns = ['Subject', listname, 'Clustering Score', subjname, 'Feature']
    elif analysis=='accuracy':
        melted_df.columns = ['Subject', listname, 'Accuracy', subjname]
    elif analysis=='temporal':
        melted_df.columns = ['Subject', listname, 'Temporal Clustering Score', subjname]
    return melted_df 
開發者ID:ContextLab,項目名稱:quail,代碼行數:28,代碼來源:helpers.py

示例10: test_top_level_method

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_top_level_method(self):
        result = melt(self.df)
        assert result.columns.tolist() == ['variable', 'value'] 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:5,代碼來源:test_melt.py

示例11: test_default_col_names

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_default_col_names(self):
        result = self.df.melt()
        assert result.columns.tolist() == ['variable', 'value']

        result1 = self.df.melt(id_vars=['id1'])
        assert result1.columns.tolist() == ['id1', 'variable', 'value']

        result2 = self.df.melt(id_vars=['id1', 'id2'])
        assert result2.columns.tolist() == ['id1', 'id2', 'variable', 'value'] 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:11,代碼來源:test_melt.py

示例12: test_value_vars

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_value_vars(self):
        result3 = self.df.melt(id_vars=['id1', 'id2'], value_vars='A')
        assert len(result3) == 10

        result4 = self.df.melt(id_vars=['id1', 'id2'], value_vars=['A', 'B'])
        expected4 = DataFrame({'id1': self.df['id1'].tolist() * 2,
                               'id2': self.df['id2'].tolist() * 2,
                               'variable': ['A'] * 10 + ['B'] * 10,
                               'value': (self.df['A'].tolist() +
                                         self.df['B'].tolist())},
                              columns=['id1', 'id2', 'variable', 'value'])
        tm.assert_frame_equal(result4, expected4) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:14,代碼來源:test_melt.py

示例13: test_value_vars_types

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_value_vars_types(self):
        # GH 15348
        expected = DataFrame({'id1': self.df['id1'].tolist() * 2,
                              'id2': self.df['id2'].tolist() * 2,
                              'variable': ['A'] * 10 + ['B'] * 10,
                              'value': (self.df['A'].tolist() +
                                        self.df['B'].tolist())},
                             columns=['id1', 'id2', 'variable', 'value'])

        for type_ in (tuple, list, np.array):
            result = self.df.melt(id_vars=['id1', 'id2'],
                                  value_vars=type_(('A', 'B')))
            tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:15,代碼來源:test_melt.py

示例14: test_single_vars_work_with_multiindex

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_single_vars_work_with_multiindex(self):
        expected = DataFrame({
            'A': {0: 1.067683, 1: -1.321405, 2: -0.807333},
            'CAP': {0: 'B', 1: 'B', 2: 'B'},
            'value': {0: -1.110463, 1: 0.368915, 2: 0.08298}})
        result = self.df1.melt(['A'], ['B'], col_level=0)
        tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:9,代碼來源:test_melt.py

示例15: test_tuple_vars_fail_with_multiindex

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import melt [as 別名]
def test_tuple_vars_fail_with_multiindex(self):
        # melt should fail with an informative error message if
        # the columns have a MultiIndex and a tuple is passed
        # for id_vars or value_vars.
        tuple_a = ('A', 'a')
        list_a = [tuple_a]
        tuple_b = ('B', 'b')
        list_b = [tuple_b]

        msg = (r"(id|value)_vars must be a list of tuples when columns are"
               " a MultiIndex")
        for id_vars, value_vars in ((tuple_a, list_b), (list_a, tuple_b),
                                    (tuple_a, tuple_b)):
            with pytest.raises(ValueError, match=msg):
                self.df1.melt(id_vars=id_vars, value_vars=value_vars) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:17,代碼來源:test_melt.py


注:本文中的pandas.melt方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。