简体   繁体   中英

how to select from a dataframe into a list where the length of the string is a certain size

I finally gave up finding a nice compact pandas answer and did this:

states_list = self.activities_df["park_states"].unique()
for state in states_list:
    if len(state) == 2:
        distinct_states.append(state)

All the other ways I tried to do this with pandas didn't work. I tried this type of logic:

states_list = df[df.column_name.str.len() == 2]
distinct_states = states_list['column_name']

I tried this type of logic:

list = df[(df['col_1'].str.len() == 2)]

I've tried multiple other things and many different combinations of the above, and I can't get it the syntax correct.

What's the best way to do what I'm wanting using pandas?

I would rewrite it as a listcomp if statement with the following syntax my_variable = [x for x in iterable if condition] :

sates_list = [distinct_states.append(state) for state in self.activities_df["park_states"].unique() if len(state) == 2]

But since I don't have more information that's the best I could come up with.

If you provide more data in your question, I'm sure we could figure it out.

You need to use .loc :

my_list = df.loc[df['col_1'].str.len() == 2, 'col_1'].values.tolist()

For example:

import pandas as pd

df = pd.DataFrame({'col_1':['a','ab','ac','abc','acb','ad']})

length_to_find = 2
my_list = df.loc[df['col_1'].str.len() == length_to_find, 'col_1'].values.tolist()

print(my_list)

Output:

['ab', 'ac', 'ad']

Note:

Depending on the contents of the column, you may need to convert the column type to a string before extracting the list, otherwise you may end up with errors.

df['col_1'] = df['col_1'].astype('str')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM