如何将调查熊猫数据框转换为可用于 Python 中的 BI 工具的不同格式?
Posted
技术标签:
【中文标题】如何将调查熊猫数据框转换为可用于 Python 中的 BI 工具的不同格式?【英文标题】:How to transform survey pandas dataframe into a different format usable with BI tools in Python? 【发布时间】:2020-07-25 13:21:55 【问题描述】:我需要将调查结果转换为可在 Tableau 等 BI 工具中使用的内容。
调查采用以下数据框的格式
df = pd.DataFrame('Respondent': ['Sally', 'Tony', 'Fred'],
'What project did you work on with - Chris?': ['Project A','Project B', np.nan],
'What score would you give - Chris': [9,7,np.nan],
'Any other feedback for - Chris': ['Random Comment','Okay performance',np.nan],
'What project did you work on with - Matt?': [np.nan,'Project C', 'Project X'],
'What score would you give - Matt': [np.nan,9,8],
'Any other feedback for - Matt': [np.nan, 'Great to work with Matt', 'Work was just okay'],
'What project did you work on with - Luke?': ['Project B','Project D', 'Project Y'],
'What score would you give - Luke': [10,8,7],
'Any other feedback for - Luke': ['Work was excellent', 'Was a bit technical', 'Another Random Comment'],
)
我需要将其转换为如下格式:
df = pd.DataFrame('Name': ['Chris','Chris','Matt','Matt','Luke','Luke','Luke'],
'Assessor': ['Sally','Tony','Tony','Fred','Sally','Tony','Fred'],
'Project Name': ['Project A', 'Project B', 'Project C', 'Project X', 'Project B', 'Project D', 'Project Y'],
'NPS Score': [9,7,9,8,10,8,7],
'Feedback': ['Random Comment','Okay performance','Great to work with Matt','Work was just okay','Work was excellent','Was a bit technical','Another Random Comment']
)
如您所见,它需要能够从列中提取名称。实际数据实际上要大得多,所以我需要代码可以处理任何大小,而不仅仅是这个示例。
【问题讨论】:
【参考方案1】:new_data = pd.DataFrame(columns = ["Assessor", "Project Name","NPS Score","Feedback", "Name"])
i = 1
while i < (len(df.columns)):
data = df.iloc[:,[0,i,i+1,i+2]]
data["Name"] = str(data.columns[-1].split(" ")[-1])
data.columns = ["Assessor", "Project Name","NPS Score","Feedback","Name"]
new_data = new_data.append(data)
i = i + 3
new_data = new_data.reset_index(drop = True)
new_data
【讨论】:
感谢 RakeshV。这似乎做到了。非常感谢:)以上是关于如何将调查熊猫数据框转换为可用于 Python 中的 BI 工具的不同格式?的主要内容,如果未能解决你的问题,请参考以下文章