【问题标题】:How to combine columns and pivot column values into new column at the same time within dataframe?如何在数据框中同时将列和枢轴列值组合到新列中?
【发布时间】:2019-01-24 01:50:59
【问题描述】:

我有一个看起来像这样的 df(10_value 列需要在 10_type 旁边,不确定如何格式化):

 0_0_type  0_0_value 0_1_type 0_1_value 0_firstname_value 0_lastname_value 10_0_type  
   uuid        1       ID         2           test1          test1    uuid   
   uuid        2    EMAIL    email1             NaN            NaN    uuid   
   uuid        3    EMAIL    email2             NaN            NaN    uuid   
   uuid        4    EMAIL    email3           test2          test2    uuid   
   uuid        5    EMAIL    email4             NaN            NaN    uuid   
   uuid        6    EMAIL    email5           test3          test3    uuid   
   uuid        7    EMAIL    email6           test4          test4    uuid   
   uuid        8    EMAIL    email7           test5          test5    uuid   
   uuid        9    EMAIL    email8           test6          test6    uuid   

   10_0_value 10_1_type  10_1_value   10_firstname_value  10_lastname_value  
        10     EMAIL     email9             test7            test7  
        11     EMAIL    email10             test8            test8  
        12     EMAIL    email11             test9            test9  
        13     EMAIL    email12            test10           test10  
        14     EMAIL    email13            test11           test11  
        15     EMAIL    email14            test12           test12  
        16     EMAIL    email15            test13           test13  
        17     EMAIL    email16            test14           test14  
        18     EMAIL    email17            test15           test15 

我有 1500k 列具有这些类型的列名结构。唯一的区别是前面的数字在变化,在本例中为010

我只想要四个数据,即uuid,email,first_name,last_name

我该怎么做:

  1. 扫描整个数据帧以查找短语 first_namelast_name 并将记录堆叠在一起

同时

  1. EMAILuuid 提取到自己的列中?

最终的 df 应该是这样的:

   uuid    EMAIL  first_name      last_name
0       1      NaN       test1          test1
1       2   email1         NaN            NaN
2       3   email2         NaN            NaN
3       4   email3       test2          test2
4       5   email4         NaN            NaN
5       6   email5       test3          test3
6       7   email6       test4          test4
7       8   email7       test5          test5
8       9   email8       test6          test6
9      10   email9       test7          test7
10     11  email10       test8          test8
11     12  email11       test9          test9
12     13  email12      test10         test10
13     14  email13      test11         test11
14     15  email14      test12         test12
15     16  email15      test13         test13
16     17  email16      test14         test14
17     18  email17      test15         test15

【问题讨论】:

  • 数据框列是否有序?
  • @W-B 它们是非常随机的,但它们中都有一个正在变化的整数。
  • 我们来看看那些以0_开头的。 0_type 和 0_value 0_type.1 和 0_value.1,是不是没有 0_firstname_value 也没有 0_lastname_value?
  • 如果不是,我们可以假设这些列是按六个一组的顺序排列的吗?
  • 有 0_first_name。我截断了列名,因为它们非常长。不,他们不会以 6 人一组的形式出现。他们分散开来。非常混乱的数据

标签: python-3.x string pandas pivot


【解决方案1】:

好的,让我们试试这些列命名的代码。首先,重命名列以将递增的索引移动到分隔符之后,“:”我选择。然后使用pd.wide_to_long 重塑数据框,最后使用filter 数据框仅选择“值”列。

df.columns = df.columns.str.replace(r'(\d+)_(\w+)',r'\2:\1')
pd.wide_to_long(df.reset_index(),
                ['0_type','1_type','0_value','1_value','firstname_value','lastname_value'],
                'index',
                'Num',
                sep=':')\
  .reset_index()\
  .filter(like='value')

输出:

    0_value  1_value firstname_value lastname_value
0         1        2           test1          test1
1         2   email1             NaN            NaN
2         3   email2             NaN            NaN
3         4   email3           test2          test2
4         5   email4             NaN            NaN
5         6   email5           test3          test3
6         7   email6           test4          test4
7         8   email7           test5          test5
8         9   email8           test6          test6
9        10   email9           test7          test7
10       11  email10           test8          test8
11       12  email11           test9          test9
12       13  email12          test10         test10
13       14  email13          test11         test11
14       15  email14          test12         test12
15       16  email15          test13         test13
16       17  email16          test14         test14
17       18  email17          test15         test15

【讨论】:

  • @RustyShackleford...我想这对你没有帮助。
  • 它很接近,但是当我运行最后一行时,我得到一个空数据框。如果我通过删除除最后一个下划线之后的字符串之外的所有字符并以这种方式连接 df 来标准化列名,您会怎么想?最后一个下划线之后的字符串是全面标准化的。
  • 你必须尝试一下。当您说最后一行时,您是在谈论'.filter ...'吗?如果是这样,您可以从返回的大型数据框中手动选择列。只需删除 .filter 并查看返回的内容。
  • 忽略。我得到了它与您的解决方案一起使用。我稍微更改了列名以匹配我所拥有的!感激不尽
猜你喜欢
  • 2015-02-08
  • 1970-01-01
  • 2021-03-12
  • 2020-11-03
  • 1970-01-01
  • 2022-01-19
  • 2015-07-20
  • 2021-09-20
  • 2016-05-22
相关资源
最近更新 更多