Slice and concatenate using pandas?

Viewed 414

My effort so far is minimal. In fact i don't know to approach it using pandas.

I got a list of even numbers and odd numbers and I need to split the list and paste it in adjacent column and do a concatenate command over the list.

for example my list reads:
odd    even
1       2
3       4
5       6
7       8
9       10
11      12
13      14
15      16
17      18  
19      20 

So I split the column exactly into two and concatenate using excel concatenate command

Expected result:
odd-odd    even-even
111         212
313         414 
515         616   
717         818 
919        1020

Help!

Minimal working code:

import pandas as pd
list2=[1,3,5,7,9,11,13,15,17,19]
df = pd.DataFrame((list2), columns=['odd'])
df = df.astype(str).groupby(df['odd'].astype(str).str[-1]).agg({"odd": lambda x: "".join(x)}).reset_index(drop=True)
print (df)

Output got:

   odd
0  111
1  313
2  515
3  717
4  919

But when I add more values to list example: list2=[1,3,5,7,9,11,13,15,17,19,21,23,25,27] i get

     odd
0  11121
1  31323
2  51525
3  71727
4    919

what i am looking for is:

115
317
519
721
923
1125
1327

This does it. Thanks to everyone for your effort.

import pandas as pd
list2=[1,3,5,7,9,11,13,15,17,19,21,23,25,27]
df = pd.DataFrame((list2), columns=['odd'])
n2 = len(df)
n1 = int(n2/2)
df2 = pd.DataFrame()
df2['odd-odd'] = df.odd[0:n1].astype(str).to_numpy()+df.odd[n1:n2].astype(str).to_numpy()
print (df2['odd-odd'])
3 Answers

Try this, convert all columns to string extract last element as reference for groupby, apply aggregate & join.

(
    df.astype(str).
        groupby(df['odd'].astype(str).str[-1]).
        agg({"odd": lambda x: "".join(x), "even": lambda x: "".join(x)}).
        reset_index(drop=True)
)

   odd  even
0  111   212
1  313   414
2  515   616
3  717   818
4  919  1020

Here's a way using numpy data transformation:

# apply function along axis

flag = np.apply_along_axis(lambda x: x <= 10, 1, df.values)
print(flag)

[[ True  True]
 [ True  True]
 [ True  True]
 [ True  True]
 [ True  True]
 [False False]
 [False False]
 [False False]
 [False False]
 [False False]]

# split the True/False in separate arrays
v1 = df.values[flag].reshape(-1, 2)
v2 = df.values[~flag].reshape(-1, 2)

print(v1)

array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])

print(v2)

array([[11, 12],
       [13, 14],
       [15, 16],
       [17, 18],
       [19, 20]])

# simply join the data and paste the columns

v3 = np.hstack([v1, v2])

v1 = np.apply_along_axis(lambda x: str(x[0])+str(x[2]), 1, v3)
v2 = np.apply_along_axis(lambda x: str(x[1])+str(x[3]), 1, v3)

df = pd.DataFrame({'odd-odd': v1, 'even-even': v2})
print(df)

  odd-odd even-even
0     111       212
1     313       414
2     515       616
3     717       818
4     919       102

You can use .to_numpy() to combine the columns. This way, you don't have to worry about the mismatched index.

df2 = pd.DataFrame()
df2['odd-odd']=df.odd[0:5].astype(str).to_numpy()+df.odd[5:10].astype(str).to_numpy()
df2['even-even']=df.even[0:5].astype(str).to_numpy()+df.even[5:10].astype(str).to_numpy()

print(df2)

Output:

  odd-odd even-even
0     111       212
1     313       414
2     515       616
3     717       818
4     919      1020

Updated with df length as a variable:

n2 = len(df)
n1 = int(n2/2)
df2 = pd.DataFrame()
df2['odd-odd']=df.odd[0:n1].astype(str).to_numpy()+df.odd[n1:n2].astype(str).to_numpy()
df2['even-even']=df.even[0:n1].astype(str).to_numpy()+df.even[n1:n2].astype(str).to_numpy()
Related