【问题标题】:How to Turn Column Labels into Values in Column?如何将列标签转换为列中的值?
【发布时间】:2019-02-22 04:16:12
【问题描述】:

我有一个 DataFrame ratings,它代表数据的原始形式。

ratings
        user_id  movie_id  rating
    32236   1      1         5
    23171   1      2         3
    83307   1      3         4
    70539   1      7         4
    13542   1      10        3
    39562   2      10        2
    172     5      2         3
    50664   6      7         2
    11223   7      7         5
    8285    7      8         5
    5643    7      10        4
    12746   8      7         3
    81332   9      7         4
    606     10     7         4

还有另一个DataFrame df_1,它是在应用ratings.pivot_table(index=['movie_id'], columns='user_id', values='rating', fill_value=0).rename_axis(None, axis=1).reset_index()之后从Dataframe派生的

df_1
   movie_id  1  2  5  6  7  8  9  10   IRAM
1     2      3  0  3  0  0  0  0   0    2
6     7      4  0  0  2  5  3  4   4    4
9     10     3  2  0  0  4  0  0   0    1
2     3      4  0  0  0  0  0  0   0    3 

如何将df_1变回ratings的形式(IRAM可以去掉)?


使用@meW 的建议的输出。

    movie_id  user_id   rating
0      9         1        5
1      5         1        3
2      2         1        3
3      2         5        3
4      9         6        4
5      9         7        5
6      9         10       4
7      9       borda      18
8      5       borda      3
9      2       borda      6
10     9        rank      3
11     5        rank      9
12     2        rank      7

【问题讨论】:

    标签: python python-3.x pandas dataframe jupyter-notebook


    【解决方案1】:

    使用melt:

    ndf = ratings.pivot_table(index=['movie_id'], columns='user_id', values='rating', fill_value=0).rename_axis(None, axis=1).reset_index()
    
    t = pd.melt(ndf, id_vars='movie_id')
    t = t[t.value != 0].reset_index(drop=True)
    t.rename(columns={'value': 'rating', 'variable': 'user_id'}, inplace=True)
    

    解决方案的验证

    df.loc[:, ['movie_id', 'user_id', 'rating']].eq(t)
    
    +----+----------+---------+--------+
    |    | movie_id | user_id | rating |
    +----+----------+---------+--------+
    |  0 | True     | True    | True   |
    |  1 | True     | True    | True   |
    |  2 | True     | True    | True   |
    |  3 | True     | True    | True   |
    |  4 | True     | True    | True   |
    |  5 | True     | True    | True   |
    |  6 | True     | True    | True   |
    |  7 | True     | True    | True   |
    |  8 | True     | True    | True   |
    |  9 | True     | True    | True   |
    | 10 | True     | True    | True   |
    | 11 | True     | True    | True   |
    | 12 | True     | True    | True   |
    | 13 | True     | True    | True   |
    +----+----------+---------+--------+
    

    【讨论】:

    • 输出不正确。标签成为user_id 下的值。
    • @JerryChen 实际上输出是正确的。您有额外的 'borda' 和 'rank' 列,它们应该被删除。
    • drop最后两列,现在它工作正常!谢谢!
    【解决方案2】:

    unstack 是您要找的吗?

    >>>df1
       movie_id  1  2  5  6  7  8  9  10
    0         2  3  0  3  0  0  0  0   0
    1         3  4  0  0  0  0  0  0   0
    2         7  4  0  0  2  5  3  4   4
    3         8  0  0  0  0  5  0  0   0
    4        10  3  2  0  0  4  0  0   0
    
    df1 = df1.set_index('movie_id').unstack().reset_index()
    df1.columns = ['user_id', 'movie_id', 'rating']
    
    >>>df1
        user_id  movie_id  rating
    0         1         2       3
    1         1         3       4
    2         1         7       4
    3         1         8       0
    4         1        10       3
    5         2         2       0
    6         2         3       0
    7         2         7       0
    8         2         8       0
    9         2        10       2
    10        5         2       3
    11        5         3       0
    12        5         7       0
    13        5         8       0
    14        5        10       0
    15        6         2       0
    16        6         3       0
    17        6         7       2
    18        6         8       0
    .
    .
    .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-18
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多