Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将旧列的值转换为新列,并将成对列的旧值用作新列中的值_Python_Pandas_Format_Schema_Data Cleaning - Fatal编程技术网

Python 将旧列的值转换为新列,并将成对列的旧值用作新列中的值

Python 将旧列的值转换为新列,并将成对列的旧值用作新列中的值,python,pandas,format,schema,data-cleaning,Python,Pandas,Format,Schema,Data Cleaning,我的任务是清理一个慈善机构设计的移动应用程序中的数据 在一个部分中,用户Q/a应用程序使用会话由一行表示。本节由重复的问题-答案字段对组成,其中一个字段表示所问的问题,然后它旁边的字段表示相应的答案。每个问题/字段和答案列对一起代表一个唯一的问题和答案 起始数据 _id answers.1.answer answers.0.fieldName answers.1.answer answers.1.fieldName 5f27f29c362a380d

我的任务是清理一个慈善机构设计的移动应用程序中的数据

在一个部分中,用户Q/a应用程序使用会话由一行表示。本节由重复的问题-答案字段对组成,其中一个字段表示所问的问题,然后它旁边的字段表示相应的答案。每个问题/字段和答案列对一起代表一个唯一的问题和答案

起始数据

_id                     answers.1.answer answers.0.fieldName answers.1.answer    answers.1.fieldName

5f27f29c362a380d3f9a9e46    1.0           enjoyment              20.0        affectedkneeside           
5f27f2ac362a380d3f9a9e4b    0.0           fatigue2weeks          40.0        avoidexercise             
5f27f4d4362a380d3f9a9e52    1.0           height                 50.0        painlocationknee

我被要求重新格式化该部分,使每个问题形成一列,相应的答案在该列中形成一个字段

理想输出

_id                    avoidexercise    enjoyment   fatigue2weeks   height
        
5f27f29c362a380d3f9a9e46    1.0           yes            20.0       120.0
5f27f2ac362a380d3f9a9e4b    0.0           no             40.0       180.0
5f27f4d4362a380d3f9a9e52    1.0           yes            50.0       150.0
我的计划是创建许多透视表,从每个Q/A列对开始,然后是concaternate(外部连接),然后是内部连接,以消除重复

但是,原始数据帧包含数字和对象数据类型的混合

因此,似乎只有一些问答列对正在转换为透视表。我尝试过使用各种聚合函数

p1 = ur.pivot_table(index=['_id'],columns= ['answers.0.fieldName'],values=['answers.0.answer'],aggfunc=lambda x: ' '.join(x))
p2 = ur.pivot_table(index=['_id'],columns= ['answers.1.fieldName'],values=['answers.1.answer'],aggfunc=lambda x: ' '.join(x))
p3 = ur.pivot_table(index=['_id'],columns= ['answers.2.fieldName'],values=['answers.2.answer'],aggfunc=lambda x: ' '.join(x))
我还尝试了另一个lambda函数

p1 = ur.pivot_table(index=['_id'],columns= ['answers.0.fieldName'],values=['answers.0.answer'],aggfunc=lambda x: ' '.join(str(v) for v in x)
到目前为止,我得到的最远的结果是使用标准平均值aggfunc运行枢轴

p1 = ur.pivot_table(index=['_id'],columns=['answers.0.fieldName'],values=['answers.0.answer'])
ps = [p1,p2,p3]
c = pd.concat(ps)
然后尝试删除合并行和列

df = c.sum(axis=1, level=1, skipna=False)

g = df.groupby('_id').agg(np.sum)
这将返回具有正确形状的数据帧

但是,它会释放对象列中的值,我不确定所有数字列的准确性

为了克服这个问题,我考虑将尽可能多的数据转换成数字

c4 = c.apply(pd.to_numeric, errors='ignore').info()
然后将组合的透视表数据框拆分为数字和对象类型

nu = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
cndf = c4.select_dtypes(include=nu)
o = ['object', 'bool', 'datetime64', 'category']
codf = c4.select_dtypes(include=o)
并在数值数据帧上运行与上述相同的.sum和groupby操作

n1 = cndf.sum(axis=1, level=1, skipna=False)
n2 = n1.groupby('_id').agg(np.sum)

但是,这仍然存在处理对象列的挑战这是另一个更优雅的解决方案。但同样不处理对象列

首先定义问题-答案对的数量:

num_answers = 2 #Following your 'Starting data' in the question
然后根据需要使用以下几行获取数据帧:

import pandas as pd

df2 = pd.concat([pd.pivot_table(df1, index=['_id'], columns= ['answers.{}.fieldName'.format(i)], values=['answers.{}.answer'.format(i)]) for i in range(num_answers)], axis = 1).fillna('N/A')
df2.columns = [col[1] for col in df2.columns]
这里,假设df1是具有起始数据的数据帧

正如您可能已经注意到的,“N/A”出现在特定id没有记录该特定字段答案的单元格中

假设三行的ID分别为[1,2,3],则“起始数据”的输出df2如下所示:

      affectedkneeside  avoidexercise   height  painlocationknee    vomitflag   weight
_id                     
0          N/A                0           N/A         N/A              0         N/A
1          N/A               N/A          156         N/A              N/A       54
2           1                N/A          N/A          3               N/A       N/A

id
字段来自哪里?亲爱的马纳金,一个好问题。我试图转换的列仅在数据帧的提取中找到。在前面的索引中可以找到_ID列。我们的示例仍然没有意义,您可以从答案1开始,然后再回答一个不相关的问题。发布一个适当的数据框架示例,只需2-3个答案即可。奇怪的是,这是数据框架的摘录。有些问题的答案是二元的。问题编码为字段名,与上一列中的答案配对