Join the first N characters of each word in a string with pandas

get first character of string python
how to get first character of a string in pandas
get first character of string column pandas
get first four characters of string pandas
pandas first letter of string column
first character pandas
pandas select first letter of string
pandas string first letter
Name=pd.Series(['Ashton Smith', 'Diego Maradona ', 'Torres, Anna'])
frame ={'InputNames':Name}
result = pd.DataFrame(frame)
result1=result.InputNames.str.split(expand=True)
result['name0']=result1[0]
result['name1']=result1[1]
result

I am trying to select the first three letters in both first name and last name and then combine it.

However, there is a problem when I def my function.

def my_fun(x, var1, var2, var3):
    print (x)
    if x[var1].astype(str).str[-1] ==',' :
        x[var3]=x[var1].astype(str).str[0:3]+x[var2].astype(str).str[0:3]
    else:
        x[var3]=x[var2].astype(str).str[0:3]+x[var1].astype(str).str[0:3]
    return x 

print (result.apply(lambda x: my_fun(x, 'name0', 'name1','ShortName'), axis=1))
<ipython-input-98-24dd0de94bd1> in my_fun(x, var1, var2, var3)
      1 def my_fun(x, var1, var2, var3):
      2     print (x)
----> 3     if x[var1].astype(str).str[-1] ==',' :
      4         x[var3]=x[var1].astype(str).str[0:3]+x[var2].astype(str).str[0:3]
      5     else:

AttributeError: ("'str' object has no attribute 'astype'", 'occurred at index 0')

Can anyone help me?

I would iterate through the dataframe simply. It might not optimal if you have big data frame.

new_column = []
n = 3 #number of characters you want to split and concanate
for i in result.loc[:, 'InputNames']:
    if ',' in i:
        concanated_string = i.split(', ')[0][:n] + i.split(', ')[1][:n]
    else:
        concanated_string = i.split()[1][:n] + i.split()[0][:n]
    new_column.append(concanated_string)

result['first_last'] = new_column

Python: How to get first N characters in a string? – thispointer.com, This N can be 1 or 3 etc. Python string is a sequence of characters and each character in it has an index number associated with it. For example,� Extract first n Characters from left of column in pandas: str [:n] is used to get first n characters of column in pandas. 1. 2. df1 ['StateInitial'] = df1 ['State'].str[:2] print(df1) str [:2] is used to get first two characters of column in pandas and it is stored in another column namely StateInitial so the resultant dataframe will be.

Split it up and then join it back. No need for apply here.

s = pd.Series(['Ashton Smith', 'Diego Maradona ', 'Torres, Anna'])

s1 = s.str.strip().str.split(r'[,\s]+')  
s1.str[0].str[:3] + '-' + s1.str[1].str[:3]

0    Ash-Smi
1    Die-Mar
2    Tor-Ann
dtype: object

Extract first n characters from left of column in pandas python , how to return first n characters from left of column in pandas python with an example . First n character from left of the column in pandas python can be .. To get the first N characters of the string, we need to pass start_index_pos as 0 and end_index_pos as N i.e. sample_str[ 0 : N ] The value of step_size will be default i.e. 0. It will slice the string from 0 th index to n-1-th index and returns a substring with first N characters of the given string. Let’s use this,

Names are messy. E.g. your dataframe has one case where the last name comes first. Furthermore, what if you have names with only two letters like Ng? How do you deal with middle names?? Try avoiding looping through the values of your dataframe. Rather use DataFrame().apply():

func = lambda x: ''.join([i[:3] for i in x.strip().split(' ')])
result.InputNames.apply(func)

0    AshSmi
1    DieMar
2    TorAnn

Python, Since strings are also array of character (or List of characters), hence when this method is applied on a series of strings, the string is joined at every character with� Or we can take just the first character of the string and change it to uppercase using the upper() method. So, to convert the first and last character of each word to upper case in a series we will be using a similar approach. First of all, let’s create a series in Pandas. Example : Let’s create a Pandas Series

Python 3 Notes: Split and Join, .split() splits mary on whitespce, and the returned result is a list of words in mary. Whitespace characters include space ' ', the newline character '\n', and tab '\t', Called on a "separator" string 'x', 'x'.join(y) joins every element in the list y� To calculate the numbers of characters we use Series.str.len(). This function returns the count of the characters in each word in a series. Syntax: Series.str.len() Return type: Series of integer values. NULL values might be present too depending upon caller series. Another way to find the number of

pandas.Series.str.slice — pandas 1.1.0 documentation, Slice substrings from each element in the Series or Index. Parameters. startint Series or Index from sliced substring from original string object. See also. Converts first character of each word to uppercase and remaining to lowercase. Series.str.capitalize. Converts first character to uppercase and remaining to lowercase. Series.str.swapcase. Converts uppercase to lowercase and lowercase to uppercase. Series.str.casefold. Removes all case distinctions in the string.

Working with text data — pandas 1.1.0 documentation, Currently, the performance of object dtype arrays of strings and arrays. In [43]: s2.str.split('_', expand=True, n=1) Out[43]: 0 1 0 a b_c 1 c d_e 2 <NA> can set the optional regex parameter to False , rather than escaping each character. to concatenate a Series or Index , either with itself or others, all based on cat() , resp . Extract first n characters from left of column in pandas python; Extract last n characters from right of the column in pandas python; Replace a substring of a column in pandas python; Regular expression Replace of substring of a column in pandas python; Repeat or replicate the rows of dataframe in pandas python (create duplicate rows)

Comments
  • Can you share the output you want?
  • The names in your dataframe have different formating. E.g. the last row has the last name first. Do you care about such cases?
  • need last_name_three_chars+first_name_three_chars. the order is different when it has a comma. that's why I did if function
  • I used if statement to consider comma. Please see below, if that is what you want?
  • need last_name_three_chars+first_name_three_chars. the order is different when it has a comma.
  • need last_name_three_chars+first_name_three_chars. the order is different when it has a comma
  • @user12533116 If you understand the code, you would know you can just swap the order in which the chars are concatenated... right? I shouldn't have to explain that ;-) s1.str[1].str[:3] + '-' + s1.str[0].str[:3]