how to remove brackets from these individual elements? [duplicate]

2024/11/15 0:36:06

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.

Answer

You can try this,

variable = [['US'], ['CA'], ['GB'], ['FR'], ['AU']] 
new_var = list(map(lambda x: x[0], variable))
print(new_var)
https://en.xdnf.cn/q/119742.html

Related Q&A

First project alarm clock

from tkinter import * from tkinter import ttk from time import strftime import winsoundclock = Tk()clock.title("WhatAClock")clock.geometry("300x400")notebook = ttk.Notebook()tab1_t…

Invalid Syntax using @app.route

Im getting a Invalid Syntax in line 22 @app.route(/start) and really dont know why... Im developing it under a Cloud9 server https://c9.io , maybe that has something to do with it... I tried it in two …

How do I count unique words using counter library in python?

im new to python and trying various librariesfrom collections import Counter print(Counter(like baby baby baby ohhh baby baby like nooo))When i print this the output I receive is:Counter({b: 10, : 8, …

Need some debugging in my program: filling up SQL tables with data retrieved from a Python program

I am filling up SQL tables with data that I have retrieved from a Python program. I am using Visual Studio Code for the Python program and MySQL Workbench 8.0 for SQL. There are some errors in it that …

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the egg…

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…