Reputation: 15
I have a dataframe with 1 million+ records and I am looking to combine two columns to one row with a separator, anyone help me how to do it ?
def chunk_results(df):
n =0
for i in range(len(df)):
data_frame = df.iloc[n:n+5]
# code for combie
n=n+5
My Dataframe:
ID Value
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
........
........
........
999,995 xxv
999,996 xxw
999,997 xxx
999,998 xxy
999,999 xxz
,and I need something like this
ID Value
1,2,3,4,5 a,b,c,d,e
6,7,8,9,10 f,g,h,i,j
........
........
........
999,995, 999,996, 999,997, 999,998, 999,999 xxv, xxw, xxx, xxy, xxz
I am passing chunksize data already into this function chunk_results as these df[values] is a value to one of the request to API so I want to send it as http://api.com?value=a,b,c,d,e so that I can post multiple values at once, I dont want to post one request at once where I have network latency
Upvotes: 1
Views: 324
Reputation: 10960
groupby
whatever the rows you want and just aggregate them
chunksize = 5
df.astype(str).groupby(df.index // chunksize).agg(','.join)
Output
ID Value
0 1,2,3,4,5 a,b,c,d,e
1 6,7,8,9,10 f,g,h,i,j
Upvotes: 2