Home > OS >  Keep the elements with particular letters in a dataframe
Keep the elements with particular letters in a dataframe

Time:01-04

I have a list:

list=['Nikolas', 'Niki', 'Niko', 'George', 'Kate']

and I want to keep only the names that have the letters "Nik" ('Nikolas', 'Niki', 'Niko')

I tried this code, but I get an error "TypeError: argument of type 'int' is not iterable".

list=['Nikolas', 'Niki', 'Niko', 'George', 'Kate']

df_1 = pd.DataFrame(list, columns =['name'])

df_1_transposed = df_1.T

df_2 = [colname for colname in df_1_transposed.columns if 'Nik' in colname]

Do you know how to fix it?

thanks in advance!

CodePudding user response:

Code

import pandas as pd
list=['Nikolas', 'Niki', 'Niko', 'George', 'Kate']
df_1 = pd.DataFrame(list, columns =['name'])
df_1 = df_1[df_1['name'].str.find('Nik') == 0]

returns

      name
0  Nikolas
1     Niki
2     Niko
  • Related