Get last column after .str.split() operation on column in pandas DataFrame
Do this:
In [43]: temp2.str[-1]
Out[43]:
0 p500
1 p600
2 p700
Name: ticker
So all together it would be:
>>> temp = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
>>> temp['ticker'].str.split(' ').str[-1]
0 p500
1 p600
2 p700
Name: ticker, dtype: object
Split pandas column and add last element to a new column
You need another str
to access the last splits for every row, what you did was essentially try to index the series using a non-existent label:
In [31]:
df['lastname'] = df['fullname'].str.split().str[-1]
df
Out[31]:
fullname lastname
0 martin master master
1 andreas test test
Python - Get Last Element after str.split()
Use a list comprehension to take the last element of each of the split strings:
ids = [val[-1] for val in your_string.split()]
splitting at underscore in python and storing the first value
Just use the vectorised str
method split
and use integer indexing on the list to get the first element:
In [228]:
df['first'] = df['construct_name'].str.split('_').str[0]
df
Out[228]:
construct_name first
0 aaaa_t1_2 aaaa
1 cccc_t4_10 cccc
2 bbbb_g3_3 bbbb
Creating a new column with last 2 values after a str.split operation
I believe you need str.split
, select last to lists and last str.join
:
df['my_col'] = df.category.str.split(',').str[-2:].str.join(',')
print (df)
category product_name my_col
0 sweet beverage, cola,sugared coca-cola cola,sugared
1 healthy,salty snacks salted pistachios healthy,salty snacks
2 juice,beverage,sweet fruit juice beverage,sweet
3 fruit juice,beverage lemon tea fruit juice,beverage
4 appetizer,salty crackers roasted peanuts appetizer,salty crackers
EDIT:
In my opinion pandas str
text functions are more recommended as apply
with puru python string functions, because also working with NaN
s and None
.
raw_data = {
'category': [np.nan, 'healthy,salty snacks'],
'product_name': ['coca-cola', 'salted pistachios']}
df = pd.DataFrame(raw_data)
df['my_col'] = df.category.str.split(',').str[-2:].str.join(',')
print (df)
category product_name my_col
0 NaN coca-cola NaN
1 healthy,salty snacks salted pistachios healthy,salty snacks
df['my_col'] = df.category.apply(lambda s: ','.join(s.split(',')[-2:]))
AttributeError: 'float' object has no attribute 'split'
split the last string after delimiter without knowing the number of delimiters available in a new column in Pandas
You can do a rsplit
, then extract the last element:
df['Column X'].str.rsplit('.', 1).str[-1]
Equivalently, you can apply the python function(s):
df['Column X'].apply(lambda x: x.rsplit('.',1)[-1])
Alternatively, you can extract
a regex pattern:
df['Column X'].str.extract('([^.]+)$', expand=False)
Output:
0 price
1 stock
2 price
3 stock
4 price
Name: Column X, dtype: object
Pandas: Split string on last occurrence
I think need indexing by str working with iterables:
#select last lists
df_client["Subject"].str.rsplit("-", 1).str[-1]
#select second lists
df_client["Subject"].str.rsplit("-", 1).str[1]
If performance is important use list comprehension
:
df_client['last_col'] = [x.rsplit("-", 1)[-1] for x in df_client["Subject"]]
print (df_client)
Subject last_col
0 Activity-Location-UserCode UserCode
1 Activity-Location-UserCode UserCode
How to select the last column of dataframe
Use iloc and select all rows (:
) against the last column (-1
):
df.iloc[:,-1:]
Splitting a column in dataframe using str.split function
Another way around to achieve this as follows..
Example DatatSet:
>>> df = pd.DataFrame({'Name': ['Karn,Kumar', 'John,Jimlory']})
>>> df
Name
0 Karn,Kumar
1 John,Jimlory
Result:
You can assign the column name while splitting the values as below.
>>> df[['First Name','Last Name']] = df['Name'].str.split(",", expand=True)
>>> df
Name First Name Last Name
0 Karn,Kumar Karn Kumar
1 John,Jimlory John Jimlory
Or, as another answer stated..
>>> df['Name'].str.split(",", expand=True).rename({0: 'First_Name', 1: 'Second_Name'}, axis=1)
First_Name Second_Name
0 Karn Kumar
1 John Jimlory
OR
>>> df['Name'].str.rsplit(",", expand=True).rename(columns={0:'Fist_Name', 1:'Last_Name'})
Fist_Name Last_Name
0 Karn Kumar
1 John Jimlory
Note: you can use axis = columns
or axis =1
both are same.
Just another way using Series.str.partition
with little altercation, However, we have to use drop
as partition
preserves the comma "," as well as a column.
>>> df['Name'].str.partition(",", True).rename(columns={0:'Fist_Name', 2:'Last_Name'}).drop(columns =[1])
Fist_Name Last_Name
0 Karn Kumar
1 John Jimlory
Just make it slim, we can define dict values for the rename
.
1 - using str.partition
..
dict = {0: 'First_Name', 2: 'Second_Name'}
df = df['Name'].str.partition(",", True).rename(dict2,axis=1).drop(columns =[1])
print(df)
First_Name Second_Name
0 Karn Kumar
1 John Jimlory
2 - using str.split()
..
dict = {0: 'First_Name', 1: 'Second_Name'}
df = df['Name'].str.split(",", expand=True).rename(dict, axis=1)
print(df)
First_Name Second_Name
0 Karn Kumar
1 John Jimlory
Related Topics
How Does the Key Argument in Python's Sorted Function Work
How to Check If Character in a String Is a Letter? (Python)
How to Access Function Variables in Another Function
Typeerror: Unsupported Operand Type(S) for /: 'Str' and 'Str'
Concatenate Two Numpy Arrays Vertically
Underscore VS Double Underscore with Variables and Methods
Python - Add Pythonpath During Command Line Module Run
Nested Dictionary to Multiindex Dataframe Where Dictionary Keys Are Column Labels
How Does _Contains_ Work for Ndarrays
How to Move Pandas Data from Index to Column After Multiple Groupby
What's the Difference Between _Builtin_ and _Builtins_
How to Automatically Fix an Invalid JSON String
How to Use Virtualenv with Python
Running Get_Dummies on Several Dataframe Columns