Home > OS >  Convert every row in a column to a list in dataframe
Convert every row in a column to a list in dataframe

Time:02-11

Data frame head

The df contains a recommended game column where , which looks like below

for ex: 1 a,b,c,d

required output : 1 [ a,b,c,d]

for every rows

i was using the below code

*x=df.games.to_list()


for k in x:
    print(k.split())
    print('****')

but i am getting the single quote at the start..*

output of the above code

['pop_vs10madame_prg,gpas_krhiddent_pop,gpas_tsgift_pop,gpas_jmoon_pop,pop_9383_pngifo,gpas_ttreasure_pop,pop_b55f9d93_blp,pop_bp_fishfrmgw_blp,athn,pop_2307a477_prg,gpas_lottiger_pop,gpas_lotwild2_pop,pop_c0556672_rdt,pop_00087_ntn,pop_32223f26_prg']


['33425,17671,17387,33035,35693,17705,6652,17685,17693,17677,17679,34845,17389,33349,17347']

CodePudding user response:

I hope it helps:

x=df.games.values
item_list = []
for item in x.split(','):
    item_list.append(item)
    print(item)

CodePudding user response:

Have you seen this answer https://stackoverflow.com/a/22341390/18089205 from the "Get list from pandas dataframe column or row?" question ?

It might answer yours.

  • Related