My pandas dataframe contains a column "file" which are strings with a file path. I am trying to use dfply to mutate this column like
resultstatsDF.reset_index() >> mutate(dirfile = os.path.join(os.path.basename(os.path.dirname(X.file)),os.path.basename(X.file)))
but I get the error
TypeError: __index__ returned non-int (type Call)
What did I do wrong? How do I do it right?
Since my question was up-voted, I guess, it is still interesting to some people. Having learned quite a bit in Python so far, let me answer it, maybe it is going to be helpful to other users.
First, let us import the required packages
and make the required pandas DataFrame
which is
We see that we still get an error (though it changed due to continuous development of dfply):
The reason is, because mutate works on series, but we need a function working on elements. Here we can use the function pandas.Series.apply of pandas, which works on series. However, we also need a custom function that we can apply on each element of the series
file. Everything put together we end up with the codewhich outputs
Doing this without dfply's
mutate, we could write alternatively