【问题标题】:how to extract column name for melt function? Python如何提取熔化功能的列名? Python
【发布时间】:2023-03-13 09:14:01
【问题描述】:

我有一个包含以下列的数据集。

data.columns[1:]
Index(['Fraud (i.e. fabricated or falsified results)',
       'Pressure to publish for career advancement',
       'Insufficient oversight/mentoring by lab principal investigator (e.g. reviewing raw data)',
       'Insufficient peer review of research',
       'Selective reporting of results',
       'Original findings not robust enough because not replicated enough in the lab publishing the work',
       'Original findings obtained with low statistical power/poor statistical analysis',
       'Mistakes or inadequate expertise in reproduction efforts',
       'Raw data not available from original lab',
       'Protocols, computer code or reagent information insufficient or not available from original lab',
       'Methods need 'green fingers' – particular technical expertise that is difficult for others to reproduce',
       'Variability of standard reagents', 'Poor experimental design',
       'Bad luck'],
      dtype='object')

而且我想用列来做融化功能,所以我做了下面的代码。

data_melt = pd.melt(data, id_vars =['respid'], value_vars =['Fraud (i.e. fabricated or falsified results)',
 'Pressure to publish for career advancement',
 'Insufficient oversight/mentoring by lab principal investigator (e.g. reviewing raw data)',
 'Insufficient peer review of research',
 'Selective reporting of results',
 'Original findings not robust enough because not replicated enough in the lab publishing the work',
 'Original findings obtained with low statistical power/poor statistical analysis',
 'Mistakes or inadequate expertise in reproduction efforts',
 'Raw data not available from original lab',
 'Protocols, computer code or reagent information insufficient or not available from original lab',
 "Methods need 'green fingers' – particular technical expertise that is difficult for others to reproduce",
 'Variability of standard reagents',
 'Poor experimental design','Bad luck'],var_name = 'factor', value_name = 'rate')

基本上,我只是将列名粘贴到 value_vars 中。

我的问题是,是否可以编写代码来实现相同的功能?

例如,只需编写如下代码。 (我知道这是错误的。)

data_melt = pd.melt(data, id_vars =['respid'], value_vars = data.columns(), ,var_name = 'factor', value_name = 'rate')

谢谢!

【问题讨论】:

  • 你试过value_vars = data.columns[1:]吗?
  • 谢谢,它有效(我以为我以前尝试过..)
  • 好的,我来回答一下。

标签: python melt


【解决方案1】:

如果 data.columns[1:] 是您需要的 values_vars,您只需将其作为参数即可:

data_melt = pd.melt(data, id_vars =['respid'], value_vars = data.columns[1:], ,var_name = 'factor', value_name = 'rate')

【讨论】:

    【解决方案2】:

    这里有一个解决方案:

    # Create a dummy dataframe with columns similar to yours. 
    df = pd.DataFrame({"respid": range(5),
                       "Fraud (i.e. fabricated or falsified results)": range(5,10), 
                       'Pressure to publish for career advancement': range(10, 15), 
                       'Insufficient oversight/mentoring by lab principal investigator (e.g. reviewing raw data)': range(15,20), 
                       'Insufficient peer review of research': range(20,25)
                      })
    
    pd.melt(df, id_vars =['respid'], value_vars=set(df.columns).difference(["respid"]))
    

    结果是:

        respid                                           variable  value
    0        0       Fraud (i.e. fabricated or falsified results)      5
    1        1       Fraud (i.e. fabricated or falsified results)      6
    2        2       Fraud (i.e. fabricated or falsified results)      7
    3        3       Fraud (i.e. fabricated or falsified results)      8
    4        4       Fraud (i.e. fabricated or falsified results)      9
    5        0               Insufficient peer review of research     20
    6        1               Insufficient peer review of research     21
    7        2               Insufficient peer review of research     22
    8        3               Insufficient peer review of research     23
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2019-01-02
      相关资源
      最近更新 更多