【问题标题】:Indexing new dataframes into new columns with pandas使用 pandas 将新数据帧索引到新列中
【发布时间】:2019-11-26 21:41:12
【问题描述】:

我需要通过选择多个列从现有数据框创建一个新数据框,并将这些列值附加到新列,并将其对应的索引作为新列

所以,假设我有这个作为数据框:

A B C D E F
0 1 2 3 4 0
0 7 8 9 1 0
0 4 5 2 4 0

通过选择列 B 到 E 转换为该列:

A index_value
1 1
7 1
4 1
2 2
8 2
5 2
3 3
9 3
2 3
4 4
1 4
4 4

因此,对于新数据帧,A 列将是旧数据帧中B through E 列的所有值,index_value 列将对应于所选列的索引值 [从零开始] .

几个小时以来,我一直在摸不着头脑。任何帮助将不胜感激,谢谢!

Python3,使用 pandas 和 numpy 库。

【问题讨论】:

    标签: python python-3.x pandas numpy


    【解决方案1】:

    尝试使用:

    df = pd.melt(df[['B', 'C', 'D', 'E']])
    # Or df['variable'] = df[['B', 'C', 'D', 'E']].melt()
    df['variable'].shift().eq(df['variable'].shift(-1)).cumsum().shift(-1).ffill()
    print(df)
    

    输出:

        variable  value
    0        1.0      1
    1        1.0      7
    2        1.0      4
    3        2.0      2
    4        2.0      8
    5        2.0      5
    6        3.0      3
    7        3.0      9
    8        3.0      2
    9        4.0      4
    10       4.0      1
    11       4.0      4
    

    【讨论】:

    • 谢谢,愚蠢的问题,但我可以使用df = pd.melt(df.iloc[:, [1,4]) 作为按索引选择列 [1,2,3,4] 的方法吗?
    • @TylerWayne 是的,这也有效,如果他们有效,请不要忘记接受我或 WenYoBen 的回答
    • 0,1.0 1,2.0 2,3.0 3,4.0 4,5.0 5,6.0 6,7.0 7,8.0 8,9.0
    • 我相信可能没有正确选择列,很快就会更新
    【解决方案2】:

    这只是melt

    df.columns = range(df.shape[1])
    s = df.melt().loc[lambda x : x.value!=0]
    s
        variable  value
    3          1      1
    4          1      7
    5          1      4
    6          2      2
    7          2      8
    8          2      5
    9          3      3
    10         3      9
    11         3      2
    12         4      4
    13         4      1
    14         4      4
    

    【讨论】:

      【解决方案3】:
      #Another way
      
          A   B   C   D   E   F
      0   0   1   2   3   4   0
      1   0   7   8   9   1   0
      2   0   4   5   2   4   0
      
      # Select columns to include
      
      start_colum ='B'
      end_column ='E'
      index_column_name ='A'
      
      #re-stack the dataframe
      
      df = df.loc[:,start_colum:end_column].stack().sort_index(level=1).reset_index(level=0, drop=True).to_frame()
      
      #Create the "index_value" column 
      
      df['index_value'] =pd.Categorical(df.index).codes+1
      
      df.rename(columns={0:index_column_name}, inplace=True)
      
      df.set_index(index_column_name, inplace=True)
      
      df
      
          index_value
      A   
      1   1
      7   1
      4   1
      2   2
      8   2
      5   2
      3   3
      9   3
      2   3
      4   4
      1   4
      4   4
      

      【讨论】:

      • 所以,我尝试了这个,但显然我不能这样做 df = df.loc[:,start_colum:end_column].stack().sort_index(level=1).reset_index(level=0, drop=True).to_frame() 因为我正在尝试 df.loc[:,9:15] 按索引选择列 9,10,11,12,13,14,15。我怎样才能按索引选择列,并且仍然可以正常工作?在我实际的 csv 文件中,没有任何标题可供选择,因为它们都是整数。
      • 参考:TypeError: cannot do slice indexing on <class "pandas.core.indexes.base.Index"> with these indexers [9] of <class "int">
      • 如果您要使用数字索引,请尝试使用 iloc[] 而不是 loc[],我在代码中使用了 loc[],因为我是按名称引用列。 'B' 而不是 1。
      • 由于某种原因,index_value 从 3 开始。我选择 iloc[9:15],输出不应该从 9 开始,数值增加到 15?
      • loc[ a:b , c:d] 表示选择 a 到 b 行和 c 到 d 列。 loc[: , c:d] 表示选择 c 到 d 的所有行和列。我爱是一样的,但基于索引。你想选择什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 2023-04-10
      • 2021-07-26
      • 2019-01-03
      • 1970-01-01
      • 2016-03-22
      相关资源
      最近更新 更多