1

I defined a function

def test_function(id):
.....
.....
.....

I was calling that function via

df = test_function('123')

but what I want to do now is, I have another Dataframe df2 =

values
123
354
645
456
176

I want to write an apply function which would take 1 value at a time from df2 and run my test_function instead of manually adding values.

and also my df should be appended for different values, such that df contain a column name 'value' which would be the value selected one at a time

2 Answers 2

1

I think you need apply with DataFrame.values and last concat:

Notice: [] are necessary, because pandas function Series.values - so df2.values return numpy array and df2['values'] select column values. Same problem is with another column names with same names as some functions in pandas like count, size, sum...

a = f2['values'].apply(test_function).values
print (pd.concat(a, ignore_index=True))
Sign up to request clarification or add additional context in comments.

10 Comments

` df['values']` or ` df2['values'] `
Sorry df2['values']
And I am not sure about df, but it seems need same like df['values'].
i'm getting result as a pandas.core.series.Series because i stored it in d3 = df2['values'].apply(test_function) df3 has 3 tables in it can i concatenate this 3 tables into one and make type(df3) as dataframe instead of series.
So need concat ? Like pd.concat([df31, df32, df33]) or pd.concat([df31, df32, df33], axis=1) ?
|
0

No need to call this function everytime, instead you do this:

df = pd.DataFrame(columns=['Val'])
df['Val']=list(df2['values'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.