python - 如何在python中多次将自定义函数应用于同一个数据框?
问题描述
我正在尝试分解熊猫的列dataframe
以制作新的columns
.
def explode(child_df, column_value):
child_df = child_df.dropna(subset=[column_value])
if isinstance(child_df[column_value].iloc[0], str):
print('tried')
child_df[column_value] = child_df[column_value].apply(ast.literal_eval)
expanded_child_df = (pd.concat({i: json_normalize(x) for i, x in child_df.pop(column_value).items()}).reset_index(level=1, drop=True).join(child_df,how='right',lsuffix='_left',rsuffix='_right').reset_index(drop=True))
expanded_child_df.columns = map(str.lower, expanded_child_df.columns)
return expanded_child_df
有没有办法将该explode
函数多次应用于数据帧,
这是我试图将explode
功能应用于数据框的地方consolidated_df
:
def clean():
column_value = ['tracking_results','trackable_items','events']
consolidated_df_cleaner = explode(consolidated_df,column_value.value)
# Need to iterate over column_value and pass the value as the second argument into `explode` function on the same dataframe
consolidated_df_cleaner.to_csv('/home/response4.csv',index=False)
试过这个但不会工作:
pd_list = []
for param in column_value:
pd_list.append(apply(explode(consolidated_df),param))
这就是我现在正在做的事情,我需要避免这种情况:
consolidated_df_cleaner=explode(consolidated_df,'tracking_results')
consolidated_df_cleaner2=explode(consolidated_df_cleaner,'trackable_items')
consolidated_df_cleaner3= explode(consolidated_df_cleaner2,'events')
consolidated_df_cleaner3.to_csv('/home/response4.csv',index=False)
预期输出:
tracking_results trackable_items events
intransit abc 22
intransit xqy 23
解决方案
尝试
(consolidated_df
.pipe(explode,'tracking_results')
.pipe(explode,'trackable_items')
.pipe(explode,'events')
.to_csv('/home/response4.csv',index=False)
)
推荐阅读
- c# - 在 Ocelot API Gateway 中添加授权标头(授权:Bearer {access_token})到特定路由
- android - 我将如何画一条线来连接颤动中的点
- curl - 在开发 C++ 中使用 curl 时出现“未定义的引用”问题
- c# - C# linq 数组到字典,各自的索引作为值中的列表/数组
- javascript - 使用 useState 破坏不可迭代实例以进行条件渲染的尝试无效
- python - PyCharm 环境 vs Matlab 环境?
- ssis - 如何在现有的 Excel 文件中为 SSIS 中的输出或目标文件创建工作表
- python - 如何快速查询 GCP 表
- javascript - 无法让 MiradorImageTools 插件工作
- java - Android 11,可以启动意图共享文件但文件未附加