【发布时间】:2016-03-30 06:34:59
【问题描述】:
关于这个错误有很多问题,但环顾四周后,我仍然无法找到/围绕解决方案。 我正在尝试使用字符串旋转数据框,以使一些行数据成为列,但到目前为止还没有解决。
我的 df 形状
<class 'pandas.core.frame.DataFrame'>
Int64Index: 515932 entries, 0 to 515931
Data columns (total 5 columns):
id 515932 non-null object
cc_contact_id 515932 non-null object
Network_Name 515932 non-null object
question 515932 non-null object
response_answer 515932 non-null object
dtypes: object(5)
memory usage: 23.6+ MB
示例格式
id contact_id question response_answer
16 137519 2206 State Ca
17 137520 2206 State Ca
18 137521 2206 State Ca
19 137522 2206 State Ca
20 137523 2208 City Lancaster
21 137524 2208 City Lancaster
22 137525 2208 City Lancaster
23 137526 2208 City Lancaster
24 137527 2208 Trip_End Location Home
25 137528 2208 Trip_End Location Home
26 137529 2208 Trip_End Location Home
27 137530 2208 Trip_End Location Home
我想转向什么
id contact_id State City Trip_End Location
16 137519 2206 Ca None None None
20 137523 2208 None Lancaster None None
24 137527 2208 None None None Home
etc. etc.
question 值成为列的位置,response_answer 在其对应列中,并保留 id
我尝试过的
unified_df = pd.DataFrame(unified_data, columns=target_table_headers, dtype=object)
pivot_table = unified_df.pivot_table('response_answer',['id','cc_contact_id'],'question')
# OR
pivot_table = unified_df.pivot_table('response_answer','question')
DataError:没有要聚合的数字类型
用字符串值旋转数据框的方法是什么?
【问题讨论】:
-
你想通过旋转字符串值来实现什么?
-
这些数据有问题。为什么同一个contact_id 会多次回答同一个问题。您是按contact_id 还是其他方式分组...什么??
-
目标是最终创建一个用于 Tableau 的报告表;将数据放在可以回答更多问题的形式中。就数据而言,缺少可以添加的细节;多个 contact_id 来自多个调查 - 未列出这些调查 id
标签: python pandas pivot-table