I have the following dataframe:
# List of Tuples
matrix = [([22, 23], [34, 35, 65], [23, 29, 31]),
([33, 34], [31, 44], [11, 16, 18]),
([44, 56, 76], [16, 34, 76], [21, 34]),
([55, 34], [32, 35, 38], [22, 24, 26]),
([66, 65, 67], [33, 38, 39], [27, 32, 34]),
([77, 39, 45], [35, 36, 38], [11, 21, 34])]
# Create a DataFrame object
df = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef'))
I'm able to apply my custom function to output start, end items in list like below for all columns:
def fl(x):
return [x[0], x[len(x)-1]]
df.apply(lambda x : [fl(i) for i in x])
But i want to apply the function to selected columns x & z.
I'm trying like below referring to this link
df.apply(lambda x: fl(x) if x in ['x', 'y'] else x)
and like this:
df[['x', 'y']].apply(fl)
How to get the output with the function applied to only x and z columns with y column unchanged.
Use DataFrame.applymap
for elementwise processing, also for last value is possible use [-1]
indexing:
def fl(x):
return [x[0], x[-1]]
df[['x', 'z']] = df[['x', 'z']].applymap(fl)
print (df)
x y z
a [22, 23] [34, 35, 65] [23, 31]
b [33, 34] [31, 44] [11, 18]
c [44, 76] [16, 34, 76] [21, 34]
d [55, 34] [32, 35, 38] [22, 26]
e [66, 67] [33, 38, 39] [27, 34]
f [77, 45] [35, 36, 38] [11, 34]
Or for solution with DataFrame.apply
use zip
with mapping tuples to list
s and selexting by str
:
def fl(x):
return list(map(list, zip(x.str[0], x.str[-1])))
df[['x', 'z']] = df[['x', 'z']].apply(fl)
print (df)
x y z
a [22, 23] [34, 35, 65] [23, 31]
b [33, 34] [31, 44] [11, 18]
c [44, 76] [16, 34, 76] [21, 34]
d [55, 34] [32, 35, 38] [22, 26]
e [66, 67] [33, 38, 39] [27, 34]
f [77, 45] [35, 36, 38] [11, 34]