【发布时间】: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 列具有这些类型的列名结构。唯一的区别是前面的数字在变化,在本例中为0 或10。
我只想要四个数据,即uuid,email,first_name,last_name。
我该怎么做:
- 扫描整个数据帧以查找短语
first_name或last_name并将记录堆叠在一起
同时
- 将
EMAIL和uuid提取到自己的列中?
最终的 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