如何修改函数中的 Pandas DataFrame 以便调用者可以看到更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35145472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify a pandas DataFrame in a function so that changes are seen by the caller?
提问by ChaimG
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)?
我发现自己对各种[pandas][1]
DataFrame执行重复性任务,因此我创建了一个函数来进行处理。如何df
在函数中进行修改,process_df(df)
以便调用者看到所有更改(不分配返回值)?
A simplified version of the code:
代码的简化版本:
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
A B 0 1 2
EDIT new code:
编辑新代码:
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
A B 0 1 2
采纳答案by Igor Raush
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,
DataFrame
使用ix
、loc
、iloc
等索引 a 会返回底层数据的视图(这是一个读取操作)。为了修改框架的内容,您需要使用就地变换。例如,
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']]