Home > Software design >  How to concatenate series in Python
How to concatenate series in Python

Time:07-18

I've the following code:

def excel_date(date1):
    temp = datetime.datetime(1899, 12, 30)
    delta = date1 - temp  if date1 != 0 else temp - temp
    return float(delta.days)   (float(delta.seconds) / 86400)

df3['SuperID'] = df3['Break_date'].apply(excel_date)
df3['SuperID2'] = df3['ticker']   str(df3['SuperID'])

Where I use a date to insert in date1 and I get a number from the excel date function.

My ticker and SuperID fields are OK:

df3 print

I want to concatenate both and get TSLA44462 BUT it's concatenating the whole series if I use str() or .astype(str) in my SuperID column.

The column types:

print dtypes

CodePudding user response:

Here my solution if I understood your problem :

import pandas as pd
df = pd.DataFrame({"Col1":[1.0,2.0,3.0,4.4], "Col2":["Michel", "Sardou", "Paul", "Jean"], "Other Col":[2,3,5,2]})
df["Concat column"] = df["Col1"].astype(int).astype(str)   df["Col2"]
df[df["Concat column"] == "1Michel"]

or

df = pd.DataFrame({"Col1":[1.0,2.0,3.0,4.4], "Col2":["Michel", "Sardou", "Paul", "Jean"], "Other Col":[2,3,5,2]})
df[(df["Col1"]==1) & (df["Col2"]=="Michel")]

CodePudding user response:

After some hours of investigation and the help of comments the way to work with series, integers, floats and strings which worked for me is this:

    def excel_date(date1):
        temp = datetime.datetime(1899, 12, 30)
        delta = date1 - temp  if date1 != 0 else temp - temp
        return float(delta.days)   (float(delta.seconds) / 86400)

First of all I convert float to integer to avoid decimals. int(x) is not feasible for series, so you better use .astype(int) which works fine.

    df3['SuperID'] = df3['Break_date'].apply(excel_date).astype(int)

After that, convert everything to char with char.array and not str(x) or .astype. You then just need to sum columns using .astype(str) to get the desired result.

    a = np.char.array(df3['ticker'].values)
    b = np.char.array(df3['SuperID'].values)
    df3['SuperID2'] = (a   b).astype(str)

Hope this help to others working with series.

regards

  • Related