当前位置: 首页>>代码示例>>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;未经允许,请勿转载。