Pandas add new columns based on splitting another column

2024/10/6 6:49:47

I have a pandas dataframe like the following:

A              B
US,65,AMAZON   2016
US,65,EBAY     2016

My goal is to get to look like this:

A              B      country    code    com
US.65.AMAZON   2016   US         65      AMAZON
US.65.AMAZON   2016   US         65      EBAY

I know this question has been asked before here and here but none of them works for me. I have tried:

df['country','code','com'] = df.Field.str.split('.')

and

df2 = pd.DataFrame(df.Field.str.split('.').tolist(),columns = ['country','code','com','A','B'])

Am I missing something? Any help is much appreciated.

Answer

You can use split with parameter expand=True and add one [] to left side:

df[['country','code','com']] = df.A.str.split(',', expand=True)

Then replace , to .:

df.A = df.A.str.replace(',','.')print (df)A     B country code     com
0  US.65.AMAZON  2016      US   65  AMAZON
1    US.65.EBAY  2016      US   65    EBAY

Another solution with DataFrame constructor if there are no NaN values:

df[['country','code','com']] = pd.DataFrame([ x.split(',') for x in df['A'].tolist() ])
df.A = df.A.str.replace(',','.')
print (df)A     B country code     com
0  US.65.AMAZON  2016      US   65  AMAZON
1    US.65.EBAY  2016      US   65    EBAY

Also you can use column names in constructor, but then concat is necessary:

df1=pd.DataFrame([x.split(',') for x in df['A'].tolist()],columns= ['country','code','com'])
df.A = df.A.str.replace(',','.')
df = pd.concat([df, df1], axis=1)
print (df)A     B country code     com
0  US.65.AMAZON  2016      US   65  AMAZON
1    US.65.EBAY  2016      US   65    EBAY
https://en.xdnf.cn/q/70400.html

Related Q&A

Proper way to insert HTML into Flask

I know how to send some plain text from Python with render_template when a URL is visited:@app.route(/success.html") ... return render_template("index.html", text="This text goes in…

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on…

Count unique elements along an axis of a NumPy array

I have a three-dimensional array likeA=np.array([[[1,1], [1,0]],[[1,2], [1,0]],[[1,0], [0,0]]])Now I would like to obtain an array that has a nonzero value in a given position if only a unique nonzero …

influxdb python: 404 page not found

I am trying to use the influxdb-python lib which I found here. But I cant even get the tutorial programm to work. When I run the following example code:$ python>>> from influxdb import InfluxD…

Django Table already exist

Here is my Django Migration file. When I run python manage.py makemigrations/migrate I get this error.Error:-django.db.utils.OperationalError: (1050, "Table tickets_duration already exists")I…

Python round() too slow, faster way to reduce precision?

I am doing the following:TOLERANCE = 13 some_float = ... round(some_float, TOLERANCE)This is run many times, so performance is important. I have to round some_float due to floating point representation…

Reading .doc file in Python using antiword in Windows (also .docx)

I tried reading a .doc file like - with open(file.doc, errors=ignore) as f:text = f.read()It did read that file but with huge junk, I cant remove that junk as I dont know from where it starts and where…

Error installing package with pip

Im trying to install a charting tool (matplotlib-v1.4.2) for python 3.4 in Windows 7, so far all my trails doesnt seem to do the job.Attempts:Ive downloaded pip from GitHub python -m pip install matplo…

Assign new values to certain tensor elements in Keras

I need to change the value of some elements of a tensor. I know what elements -- they are in a boolean tensor already.I dont see how to do this in keras code. But if I were using TensorFlow code I woul…

Making grid triangular mesh quickly with Numpy

Consider a regular matrix that represents nodes numbered as shown in the figure:I want to make a list with all the triangles represented in the figure. Which would result in the following 2 dimensional…