user3046211
user3046211

Reputation: 696

Splitting a column into multiple columns

I have a pandas dataframe as below :

|  A       |  Value |
+----------+--------+
|ABC001035 |  34    |
|USN001185 |  45    |
|UCT010.75 |  23    |
|ATC001070 |  21    |  
+----------+--------+

I want to split the column in A (based on last three digits in A) into columns X and Y, and it should look like below

|  A       |  Value |    X    | Y   |
+----------+--------+---------+-----+
|ABC001035 |  34    |  ABC001 | 035 |
|USN001185 |  45    |  USN001 | 185 |
|UCT010.75 |  23    |  UCT01  | 0.75|
|ATC001070 |  21    |  ATC001 | 070 |
+----------+--------+---------+-----+

So how to split the column A ?

Upvotes: 0

Views: 46

Answers (2)

Cimbali
Cimbali

Reputation: 11395

You can index all strings in a series with the .str accessor:

>>> df['X'] = df['A'].str[:-3]
>>> df['Y'] = df['A'].str[-3:]
>>> df
           A  Value       X    Y
0  ABC001035   34.0  ABC001  035
1  USN001185   45.0  USN001  185
2  UCT010.75   23.0  UCT010  .75
3  ATC001070   21.0  ATC001  070

Upvotes: 2

Florian Fasmeyer
Florian Fasmeyer

Reputation: 879

Split your problem into smaller ones, easier to solve! :)

How to split a string (take the last 3 characters):

'Hello world!'[-3:0]
# Returns: ld!

How to apply a function over a DataFrame value?

df.A.apply(lambda x: x[-3:])
# Returns pandas.Series: [035, 185, 0.75, 070]

How to save a Series to a new DataFrame column?

# Create Y column.
df['Y'] = df.A.apply(lambda x: x[-3:])

Upvotes: 1

Related Questions