-1

In Python, how to convert this dataframe structure

[['US'], ['CA'], ['GB'], ['FR'], ['AU']] 

So that the double nested brackets are removed:

['US', 'CA', 'GB', 'FR', 'AU'] 

Here's a bit of context. I have a df with customer and country data. I want to count the countries so can find the top5 countries, and them use that as a filter elsewhere.

this gives me the counts

countries = collections.Counter(responses_2021['country'].dropna())

that yields this

[('US', 144), ('CA', 37), ('GB', 15), ('FR', 15), ('AU', 12)]

and this gives me the top 5

countries_top5 = countries.most_common(5)

now I need to transform it into a more simple structure so I can do my filter (here i'm just typing it manually because that's the only way I could move forward lol)

options = ['US', 'CA', 'GB', 'FR', 'AU'] 
rslt_df = df[df['country'].isin(options)] 

SO to get from the this

[('US', 144), ('CA', 37), ('GB', 15), ('FR', 15), ('AU', 12)]

to this

['US', 'CA', 'GB', 'FR', 'AU'] 

I started by trying to remove the counts

countries_top5_names = np.delete(countries_top5, 1, 1)

but that yields

[['US'], ['CA'], ['GB'], ['FR'], ['AU']] 

so now I'm trying to flatten that, but I don't know how.

ChatGPT
  • 5,334
  • 12
  • 50
  • 69
  • I'd like comments with downvotes – ChatGPT Feb 27 '22 at 22:18
  • 1
    The code in your question is not a dataframe or a numpy array; it's a list of lists. What does this have to do with either Pandas or Numpy? It seems like you are just trying to flatten nested lists and there are many, many dupes for how to do that. – Mark Feb 27 '22 at 22:21
  • Questions with zero runnable code are highly likely to get downvotes. I suggest code include as much error-free code as possible. It is certainly ok to have code have the exact error that requires a solution. Suggest, edit your question with some code. Start with input_list = [['US'], ['CA'], ['GB'], ['FR'], ['AU']]. Add what you can after that. Even a comment # Desire output = ['US', 'CA', 'GB', 'FR', 'AU'] – Carl_M Feb 27 '22 at 22:23
  • [How to make a flat list out of a list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists/952952#952952) provides accomplishing this with a lambda. Another reason for downvotes is not showing results of research. Only experience would let you know to use the word "flatten". – Carl_M Feb 27 '22 at 22:29
  • x = [['US'], ['CA'], ['GB'], ['FR'], ['AU']] [i[0] for i in x] – mike Feb 27 '22 at 23:53
  • thanks everyone, I've added more context in the question if that helps determine a better solution. – ChatGPT Feb 28 '22 at 23:08

2 Answers2

3

You can try this,

variable = [['US'], ['CA'], ['GB'], ['FR'], ['AU']] 
new_var = list(map(lambda x: x[0], variable))
print(new_var)

Froggo
  • 53
  • 6
Ade_1
  • 1,480
  • 1
  • 6
  • 17
  • I upvoted this one. I hope the poster of the original question will mark one answer as accepted. – Carl_M Feb 27 '22 at 22:39
1
variable = [["US"], ["CA"], ["GB"], ["FR"], ["AU"]]
variable = [item for sublist in variable for item in sublist]
print(variable)
Parvesh Kumar
  • 1,104
  • 7
  • 16