I got a pretty big data frame from csv to pandas.
The problem is that in some columns I get lines of text that I would like to highlight for the last character in order to turn it into integers.
I found a solution, but I am sure that it is not the most effective. This happens as follows:
import pandas as pd
df = pd.read_csv("filename")
cols = list(df.loc[:, 'col_a':'column_s'])
df_filtered = df[cols].dropna()
df_filtered['col_o'] = df_filtered['col_o'].str[-1:]
df_filtered['col_p'] = df_filtered['col_p'].str[-1:]
df_filtered['col_q'] = df_filtered['col_q'].str[-1:]
df_filtered['col_r'] = df_filtered['col_r'].str[-1:]
df_filtered['col_s'] = df_filtered['col_s'].str[-1:]
From a writing point of view, this is not very effective. So I tried something like this:
colstofilter = list(df_filtered.loc[:, 'col_o':'col_s'])
for col in df_filtered[colstofilter]:
print(df_filtered[col].str[-1:].head())
Printing it gives exactly what I want, but when I try to turn it into a function or lamba or apply it to a data framework, I get an error message that is not supported
source
share