function returning pandas dataframe

非 Y 不嫁゛ 提交于 2021-02-10 11:49:42

问题


I was not clear about my issue, so I am reviewing the question. I have a function manipulating a generic dataframe (it removes and renames columns and records):

def manipulate_df(df_local):
    df_local.rename(columns={'A': 'grouping_column'}, inplace = True)
    df_local.drop('B', axis=1, inplace=True)
    df_local.drop(df.query('grouping_column not in (\'1\', \'0\')').index, inplace = True)
    df_local = df_local.groupby(['grouping_column'])['C'].sum().to_frame().reset_index().copy()

    print("this is what I need:")
    print(df_local)

this manipulating function is called in the main body of my code:

df = pd.DataFrame(np.random.randint(0,10,size=(100, 3)), columns=list('ABC'))
manipulate_df(df)
print("this is what I got:")
print(df.head(5))

Upon execution of the blocks ahead, this is the Output I am getting:

this is what I need:
   grouping_column   C
0                0  72
1                1  29
this is what I got:
   grouping_column  C
0                0  5
1                0  5
2                0  4
3                1  9
6                0  5

While the dropping and the filters are retained, the grouping is not. How can I achieve the manipulating function to actually group the dataframe?

Thanks again and apologize for my previous confusing post.


回答1:


The thing is, aside from unreadable code, that you never return changes.

Take a look at this example:

a = 'foo'

def my_func(a):
    a = 'bar'

my_func(a)
print(a)
#foo
a = my_func(a)
#None

Avoiding all the scope talk, you need to return something from your function or edit the global variable:

a = 'foo'

def my_func():
    global a
    a = 'bar'
myfunc()
print(a)
#bar

Or a = 'foo'

def my_func(a):
    a = 'bar'
    return a
a = myfunc(a)
print(a)
#bar


来源:https://stackoverflow.com/questions/47552206/function-returning-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!