Reshifting Specific Column Based On Row If String Row Match With List
Solution 1:
I see now what's causing the problem. If you look at the result of
match = df['Keterangan'].str.fullmatch('|'.join(entry for entry in my_list))
df['shift'] = match.cumsum()
df['index'] = df.index
columns = df.columns
df = df.apply(lambda row: print(row), axis='columns')
you will see something like
Q22019 22686796.0Q22018 27421625.0shift0.0index0.0Name:Kas,dtype:float64Q22019 68409507.0Q22018 71159442.0shift0.0index1.0Name:Giropadabankindonesia,dtype:float64Q22019 15675129.0Q22018 12584938.0shift1.0index2.0Name:Giropadabanklain,dtype:float64...
The rows are Series with a unified type, which is float64
here.
From the documentation:
Objects passed to the function are Series objects ...
Some further points. If you look at the simple examples
df = pd.DataFrame({'A': [1, 2], 'B': [1., 2.]})
print(df.iloc[0], 'w')
df = pd.DataFrame({'A': ['a', 'b'], 'B': [1., 2.]})
print(df.iloc[0])
you will see the following output
A1.0B1.0Name: 0, dtype: float64AaB1Name: 0, dtype: object
Both are Series. In the first, Pandas sees that all types are numeric, so it chooses the best numeric type to accomodate the types of both values, which is float
. In the second, due to the string, Pandas chooses object
, which accomodates almost all types.
In the DataFrame from your other question there's a string in the 2. and 3. column (the 'Nan'
, which isn't NaN
!), which leads to the type object
and in the following also to the type object
in the row
variables in apply
. The DataFrame here has tpye float
in the 2. and 3. column (the type of NaN
is float
) and therefore also float
in the row
variables. That's the reason why the original code worked for the first example but not here. (At least that's what I think, I could be wrong.)
I have adjusted the code in my suggestion accordingly (int
casts).
Post a Comment for "Reshifting Specific Column Based On Row If String Row Match With List"