PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, May 13, 2022

[FIXED] How do I append two columns from csv file in list?

 May 13, 2022     append, dataframe, loops, pandas, python     No comments   

Issue

My code is -

df=pd.read_csv("file path")
l1=[]
l2=[]
for i in range(0,len(df['unions']),len(df['district'])):
    l1.append((df['unions'][i], df['district'][i]))
    l2.append(({"entities": [(0,len(df['unions'][i]),df['subdistrict'][i])]}))

TRAIN_DATA=list(zip(l1,l2))
print(TRAIN_DATA)

Result I got - [(('Dhansagar', 'Bagerhat'), {'entities': [(0, 9, 'Sarankhola')]})]

But I want to get result is this format -

[(('Dhansagar Bagerhat'), {'entities': [(0, 9, 'Sarankhola')]})]

Basically no comma in between Dhansagar Bagerhat. How do I do it? Also, why am I getting only one result? It seems like my loop is not working.


Solution

Instead of:

    l1.append((df['unions'][i], df['district'][i]))

Try:

    l1.append(' '.join((df['unions'][i], df['district'][i])))

Or:

    l1.append((df['unions'][i] + " " + df['district'][i]))


Answered By - U12-Forward
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing