Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

pandas - Extract text from python list item

I've a python list whose items are as below, I want to extract only the text and remove the 0 and space from the item.

  [['0    Client Name:'],
  ['0    Client ID:'],
  ['0    Industry:'],
  ['0    SEC:'],
  ['0    Industry Sector:']]

So, how to extract only the text from each item? This list was the result of the following loop:

   '''for index,row in df.iterrows():
        if index==0:
            continue
        elif index%2!=0:
            title.append(row.to_string().split(","))'''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the following, altough not very clean but gets the job done.

l = [['0    Client Name:'],
    ['0    Client ID:'],
    ['0    Industry:'],
    ['0    SEC:'],
    ['0    Industry Sector:']]

newl = []

for i in range(len(l)):
    newl.append(l[i][0].split("  ")[2])

print(newl)
'''
['Client Name:', 'Client ID:', 'Industry:', 'SEC:', 'Industry Sector:']
'''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...