I have a pandas dataframe that I need to apply a function to. The function however, returns many items for a single row in the dataframe. I would like to create a new dataframe with the values returned from the function. So, far when I applied the function, the new dataframe had the same number of rows as the original dataframe, with the lists/series from the function as rows. What I need in the new dataframe are the values returned from the function as a column (flattned lists). Also, I would like to know whether this is a good way to use Pandas (in terms of harnessing the efficiency of the library) or will I be better off just sticking to pure python?
This is what I used:
def get_children(id):
children = []
suff = ['0', '1', '2', '3']
for s in suff:
children.append(id+s)
return pd.Series(children)
df = pd.DataFrame()
df = all_attrbs['id'].apply(get_children)
Input:
all_attrbs:
id
0
1
2
3
Expected Output:
df:
t_id
00
01
02
03
11
12
13
14
20
21
22
23
30
31
32
33
I'm not sure what your data looks like, but here is a way to do it efficiently :
df = pd.DataFrame({'id' : [0,1,2,3,4,5,6]})
flattened_list = ["{}_{}".format(x, i) for i in range(4) for x in df['id']]
df2 = pd.DataFrame(flattened_list)
Output flattened_list
:
['0_0',
'1_0',
'2_0',
'3_0',
'4_0',
'5_0',
'6_0',
'0_1',
'1_1',
'2_1',
'3_1',
'4_1',
'5_1',
'6_1',
'0_2',
'1_2',
'2_2',
'3_2',
'4_2',
'5_2',
'6_2',
'0_3',
'1_3',
'2_3',
'3_3',
'4_3',
'5_3',
'6_3']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.