python - Using Pandas convert column values to column header
问题描述
Data Set
result = [['Nikolas', 'Webber', 63, 'Male', 'Options', 'Option - 1'],
['Sanaa', 'Kirkpat', 53, 'Male', 'Options', 'Option - 3'],
['Laaibah', 'Andrson', 52, 'Male', 'Options', 'Option - 1'],
['Beck', 'Villanu', 61, 'Male', 'Options', 'Option - 2'],
['Blair', 'Hook', 67, 'Male', 'Options', 'Option - 1'],
['Kavan', 'Hodges', 47, 'Male', 'Options', 'Option - 3'],
['Kajus', 'Oco', 54, 'Male', 'Options', 'Option - 1']]
pandas DataFrame created
df = pandas.DataFrame(result, columns=['First Name', 'Last Name', 'Age', 'Gender', 'field_label', 'field_value'])
Values in column field_label has to be transposed as column header replacing column header name - field_value to Options
data = df.pivot_table(index=['First Name', 'Last Name', 'Age', 'Gender'], columns='field_label', values='field_value', fill_value='', aggfunc='first')
data = data.reset_index().rename_axis(None, axis=1)
Tried serveral other ways to achieve this using pivot_table
and aggfunc='first'
but some of the records are getting omitted when result set is too large to create dataframe.
Input
First Name Last Name Age Gender field_label field_value
Nikolas Webber 63 Male Options Option - 1
Sanaa Kirkpat 53 Male Options Option - 3
Laaibah Andrson 52 Male Options Option - 1
Beck Villanu 61 Male Options Option - 2
Blair Hook 67 Male Options Option - 1
Kavan Hodges 47 Male Options Option - 3
Kajus Oco 54 Male Options Option - 1
Any pointer to get below Output using pandas pivot_table or any other way would be really appreciated.
Output
First Name Last Name Age Gender Options
Nikolas Webber 63 Male Option - 1
Sanaa Kirkpat 53 Male Option - 3
Laaibah Andrson 52 Male Option - 1
Beck Villanu 61 Male Option - 2
Blair Hook 67 Male Option - 1
Kavan Hodges 47 Male Option - 3
Kajus Oco 54 Male Option - 1
解决方案
I think code below will help you:
# create your processing function
def handle_function(row: pd.Series) -> pd.Series:
"""
What you want to do here...
"""
# get label and value
label = row["field_label"]
value = row["field_value"]
# create column for label if it exists no create
row[label] = value
# return new row
return row
# apply on your dataframe
df.apply(handle_function, axis=1)
# remove columns for label and values
df.drop(["field_label", "field_value", axis=1, inplace=True]
推荐阅读
- html - CSS 文本转换选择器
- excel - 当代码是静态的并且控件动态创建时,控件的 VBA 用户窗体事件代码不起作用
- javascript - 需要帮助解决剪刀石头布的 Switch Case 语句
- xamarin - 为什么我在 Xamarin Forms 的 iOS 应用程序输出中收到“SecTaskLoadEntitlements failed error=22 cs_flags=200”消息?
- python - 对 50k x 50k 系统的 numpy.linalg.solve 性能改进
- java - 检查任意圆是否包含给定点集的 k 个以上的点
- opencyc - 重复的 ke-create 导致了不受欢迎的额外具体化
- python-3.x - 矢量化与非矢量化产品给出不同的结果。为什么?
- excel - Excel。日期和时间的自动单元格格式,用于更快和简化的写入过程
- postgresql - 如何在创建时在 postgres docker 映像中加载数据