我有一个表格,其中两个不同类型的列已堆叠到field
列中 - 属性和问题。
+-------+------------+-------+
| id | field | value |
+-------+------------+-------+
| 52394 | gender | M |
| 52394 | age | 24 |
| 52394 | question_1 | 2 |
| 52394 | question_2 | 1 |
+-------+------------+-------+
我想重新塑造它,以便性别和年龄成为列,而question_1和question_2仍然堆叠。
+-------+--------+-----+------------+-------+
| id | gender | age | field | value |
+-------+--------+-----+------------+-------+
| 52394 | M | 24 | question_1 | 2 |
| 52394 | M | 24 | question_2 | 1 |
+-------+--------+-----+------------+-------+
关于如何做到这一点的任何想法?
答案 0 :(得分:3)
这将是我的策略:
将视野应用于您的df,其中字段为性别或年龄,另存为df1。选择字段不是性别或年龄的df,另存为df2。然后在id上合并两个(df1和df2)。这是我的完整代码:
import pandas as pd
import sys
if sys.version_info[0] < 3:
from StringIO import StringIO
else:
from io import StringIO
# Create df
rawText = StringIO("""
id field value
52394 gender M
52394 age 24
52394 question_1 2
52394 question_2 1
""")
df = pd.read_csv(rawText, sep = "\s+")
df1 = df[df['field'].isin(['gender','age'])]
df1 = df1.pivot(index = 'id', columns = 'field', values = 'value').reset_index()
df2 = df[~df['field'].isin(['gender','age'])]
df1.merge(df2)
结果是:
id age gender field value
0 52394 24 M question_1 2
1 52394 24 M question_2 1