如何在函数中修改pandas DataFrame,以便调用者可以看到更改? [英] How to modify a pandas DataFrame in a function so that changes are seen by the caller?
问题描述
我发现自己对各种[pandas][1]
DataFrame执行重复的任务,所以我做了一个函数来进行处理.如何在函数process_df(df)
中修改df
,以便调用者看到所有更改(不分配返回值)?
I find myself doing repetitive tasks to various [pandas][1]
DataFrames, so I made a function to do the processing. How do I modify df
in the function process_df(df)
so that the caller sees all changes (without assigning a return value)?
代码的简化版本:
def process_df(df):
df.columns = map(str.lower, df.columns)
df = pd.DataFrame({'A': [1], 'B': [2]})
process_df(df)
print df
A B
0 1 2
编辑新代码:
def process_df(df):
df = df.loc[:, 'A']
df = pd.DataFrame({'A': [1], 'B': [2]})
process_df(df)
print df
A B
0 1 2
推荐答案
使用ix
,loc
,iloc
等为DataFrame
编制索引将返回基础数据的视图(这是读取操作) ).为了修改框架的内容,您将需要使用就地转换.例如,
Indexing a DataFrame
using ix
, loc
, iloc
, etc. returns a view of the underlying data (it is a read operation). In order to modify the contents of the frame you will need to use in-place transforms. For example,
def process_df(df):
# drop all columns except for A
df.drop(df.columns[df.columns != 'A'], axis=1, inplace=True)
df = DataFrame({'A':[1,2,3], 'B':[1,2,3]})
process_df(df)
要更改列的顺序,您可以执行以下操作:
To change the order of columns, you can do something like this:
def process_df(df):
# swap A and B
df.columns = ['B', 'A']
df[['B', 'A']] = df[['A', 'B']]
这篇关于如何在函数中修改pandas DataFrame,以便调用者可以看到更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!