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
Q2 2019 22686796.0
Q2 2018 27421625.0
shift 0.0
index 0.0
Name: Kas, dtype: float64
Q2 2019 68409507.0
Q2 2018 71159442.0
shift 0.0
index 1.0
Name: Giro pada bank indonesia, dtype: float64
Q2 2019 15675129.0
Q2 2018 12584938.0
shift 1.0
index 2.0
Name: Giro pada bank lain, 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
A 1.0
B 1.0
Name: 0, dtype: float64
A a
B 1
Name: 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"