Pandas Flatten a list of list within a column?

2024/4/15 0:38:33

I am trying to flatten a column which is a list of lists:

    var         var2
0   9122532.0   [[458182615.0], [79834910.0]]
1   79834910.0  [[458182615.0], [9122532.0]]
2   458182615.0 [[79834910.0], [9122532.0]]

I want:

    var         var2
0   9122532.0   [458182615.0, 79834910.0]
1   79834910.0  [458182615.0, 9122532.0]
2   458182615.0 [79834910.0, 9122532.0]

Applying

sample8['var2'] = sample8['var2'].apply(chain.from_iterable).apply(list)

Gives me:

    var1        var2
0   9122532.0   [[, 4, 5, 8, 1, 8, 2, 6, 1, 5, ., 0, ], [, 7, ...
1   79834910.0  [[, 4, 5, 8, 1, 8, 2, 6, 1, 5, ., 0, ], [, 9, ...
2   458182615.0 [[, 7, 9, 8, 3, 4, 9, 1, 0, ., 0, ], [, 9, 1, ...
Answer

Data:

In [162]: df
Out[162]:var                           var2
0    9122532.0  [[458182615.0], [79834910.0]]
1   79834910.0   [[458182615.0], [9122532.0]]
2  458182615.0    [[79834910.0], [9122532.0]]

Solution: use np.ravel():

In [163]: df['var2'] = df['var2'].apply(np.ravel)In [164]: df
Out[164]:var                       var2
0    9122532.0  [458182615.0, 79834910.0]
1   79834910.0   [458182615.0, 9122532.0]
2  458182615.0    [79834910.0, 9122532.0]
https://en.xdnf.cn/q/73337.html

Related Q&A

How to use libxml2 with python on macOs?

Im on OSX Lion and I have libxml2 installed (by default) and I have python installed (by default) but they dont talk to one another. Whats the simplest way to make this work on Lion?$ python -c "…

SMTP Authentication error while while sending mail from outlook using python language

import smtplibsmtpObj = smtplib.SMTP(smtp.office365.com, 587)smtpObj.ehlo()smtpObj.starttls()smtpObj.login([email protected], abcde)smtpObj.sendmail([email protected], [email protected], Subject: So l…

How do you change environment of Python Interactive on Vscode?

I recently migrated from Spyder to VScode. I created a new conda environment and used setting.json to change the environment in VScode, "python.pythonPath": "/Users/dcai/anaconda3/envs/…

Validate list in marshmallow

currently I am using marshmallow schema to validate the request, and I have this a list and I need to validate the content of it.class PostValidationSchema(Schema):checks = fields.List(fields.String(re…

Save unicode in redis but fetch error

Im using mongodb and redis, redis is my cache.Im caching mongodb objects with redis-py:obj in mongodb: {uname: umatch, usection_title: u\u6d3b\u52a8, utitle: u\u6bd4\u8d5b, usection_id: 1, u_id: Objec…

Authentication with public keys and cx_Oracle using Python

Ive Googled a bit but I havent found any substantial results. Is it possible to use key-based authentication to connect to an Oracle server using Python? My objective is to be able to automate some re…

No luck pip-installing pylint for Python 3

Im interested in running a checker over my Python 3 code to point out possible flaws. PyChecker does not work with Python 3. I tried to pip-install Pylint, but this fails. The error message does not he…

How to use tf.nn.embedding_lookup_sparse in TensorFlow?

We have tried using tf.nn.embedding_lookup and it works. But it needs dense input data and now we need tf.nn.embedding_lookup_sparse for sparse input.I have written the following code but get some erro…

Saving Python SymPy figures with a specific resolution/pixel density

I am wondering if there is a way to change the pixel density/resolution of sympy plots. For example, lets consider the simple code snippet below:import sympy as sypx = syp.Symbol(x) miles_to_km = x * 1…

Matplotlib boxplot width in log scale

I am trying to plot a boxplot with logarithmic x-axis. As you can see on the example below width of each box decreases because of the scale. Is there any way to make the width of all boxes same?