在使用自定义函数后如何存储新数据框? [英] How to store a new dataframe after using a self defined function on it?

查看:83
本文介绍了在使用自定义函数后如何存储新数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用用户定义的函数,所以请原谅这不是一个非常复杂的问题.

I am just starting to use user-defined functions, so this is probably not a very complex question, forgive me.

我有几个数据框,所有的数据框都有一个名为"interval_time"的列,例如,我想将此列重命名为"Timestamp".

I have a few dataframes, which all have a column named 'interval_time' (for example) and I would like to rename this column 'Timestamp'.

我知道我可以通过此方式手动执行此操作;

I know that I can do this manually with this;

df = df.rename(index=str, columns={'interval_time': 'Timestamp'})

但是现在我想定义一个名为rename的函数来为我完成此任务.我已经看到这行得通;

but now I would like to define a function called rename that does this for me. I have seen that this works;

def rename(data):
    print(data.rename(index=str, columns={'interval_time': 'Timestamp'}))

但是我似乎无法弄清楚保存重命名的数据框,我已经尝试过了;

but I can't seem to figure out to save the renamed dataframe, I have tried this;

def rename(data):
    data = data.rename(index=str, columns={'interval_time': 'Timestamp'})

我正在使用的数据帧具有以下形式;

The dataframes that I am using have the following form;

df_scada
              interval_time                 A         ...             X                 Y 
0       2010-11-01 00:00:00                0.0        ...                396.36710         381.68860
1       2010-11-01 00:05:00                0.0        ...                392.97974         381.40634
2       2010-11-01 00:10:00                0.0        ...                390.15695         379.99493
3       2010-11-01 00:15:00                0.0        ...                389.02786         379.14810

推荐答案

有几点需要注意:

  • 您需要在函数中使用return.
  • 使此类函数通用是一种很好的做法.例如,您的输入和输出列名称可以是设置了默认值的参数.
  • 熊猫提供 pd.DataFrame.pipe 以便于方法链接.
  • 您不应将函数的名称与Pandas方法的名称相同.这只会导致混乱.
  • You need to use return in your function.
  • It's good practice to make such functions generic. For example, your input and output column names can be arguments with default values set.
  • Pandas offers pd.DataFrame.pipe to facilitate method chaining.
  • You should not name your function the same as the Pandas method. This will only lead to confusion.

将这些元素放在一起:

def rename_col(data, col_in='interval_time', col_out='Timestamp'):
    return data.rename(index=str, columns={col_in: col_out})

df = df.pipe(rename_col)

这是一个简单的示例,它可能不需要一个用户定义的函数.但是,当您编写更复杂的函数时,上述建议可能会有所帮助.

This is a trivial example, which probably doesn't require a user-defined function. However, the above advice may help when you write more complex functions.

这篇关于在使用自定义函数后如何存储新数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆