我需要将初始列的组融为归一化程度不好的数据集中的多个目标列。这是一个示例(来自此问题pandas dataframe reshaping/stacking of multiple value variables into seperate columns):

         des1 des2 des3 interval1 interval2 interval3
value
aaa       a    b    c     ##1         ##2       ##3
bbb       d    e    f     ##4         ##5       ##6
ccc       g    h    i     ##7         ##8       ##9


我正在尝试将其融合为以下方向:

         des      interval
value
aaa       a         ##1
aaa       b         ##2
aaa       c         ##3
bbb       d         ##4
bbb       e         ##5
bbb       f         ##6
ccc       g         ##7
ccc       h         ##8
ccc       i         ##9


我希望使用melt而不是stack来避免手动设置大量数据。到目前为止,这是我的开始:

import pandas as pd
import numpy as np
import fnmatch

column_list = list(df_initial.columns.values)

question_sources = [c for c in fnmatch.filter(column_list, "measure*question*source")]
question_ranks = [c for c in fnmatch.filter(column_list, "measure*rank")]
question_targets = [c for c in fnmatch.filter(column_list, "measure*targeted")]
question_statuses = [c for c in fnmatch.filter(column_list, "measure*status")]

place = [c for c in fnmatch.filter(column_list, "place")]
measure_statuses = [c for c in fnmatch.filter(column_list, "measureInfo_status")]

starter_list = place + measure_statuses

df_gpro_melt_1 = (pd.melt(df_initial, id_vars=starter_list,
                    value_vars=question_sources, var_name="question_sources",
                    value_name="question_sources_values"))


是否可以将初始列组融合为多个目标列?任何建议深表感谢。

最佳答案

如果您的列遵循示例数据框中的模式,则此方法适用于您的示例:

pd.concat((pd.DataFrame({'des':df.iloc[:,i],
                         'interval':df.iloc[:,i+3]})
             for i in range(3)))


如果两对不同,则可以使用此模式,但要遍历列表

tuples = [(0,3),(1,4),(2,5)]

pd.concat((pd.DataFrame({'des':df.iloc[:,i],
                          'interval':df.iloc[:,j]})
             for i,j in tuples))

关于python - Python Pandas将初始列组融为多个目标列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35187963/

10-12 19:24