How to change the full text of some columns in pandas

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

+6
source share
2

:

df_filtered.loc[:, 'col_o':'col_s'] = \
    df_filtered.loc[:, 'col_o':'col_s'].apply(lambda x: x.str[-1])
+3

df

from string import ascii_lowercase

df = pd.DataFrame(
    'ABC', list('xyz'),
     list(ascii_lowercase[:10])
).add_prefix('col_')

df

  col_a col_b col_c col_d col_e col_f col_g col_h col_i col_j
x   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC
y   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC
z   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC   ABC

update + loc + stack/str/unstack
stack, , str .
unstack, .
update df

df.update(df.loc[:, 'col_d':'col_g'].stack().str[-1].unstack())

df

  col_a col_b col_c col_d col_e col_f col_g col_h col_i col_j
x   ABC   ABC   ABC     C     C     C     C   ABC   ABC   ABC
y   ABC   ABC   ABC     C     C     C     C   ABC   ABC   ABC
z   ABC   ABC   ABC     C     C     C     C   ABC   ABC   ABC
+1

Source: https://habr.com/ru/post/1016614/


All Articles