1

I've been getting lists in emails with line breaks for each item what I want to do is just copy and paste then save as csv and have a script create a python list out of the data. Since it's columns from the paste to the file I'm getting improper formatting, if I change I do a replace on the line breaks for commas it look a bit better but still since it's converted from a column it's not correct and with mylist.values.tolist() it still doesn't work. Here's what I have and what's happening. I can do this with the builtin csv library but was wondering how to do it in p

Email format that I'm pasting:

Fox Chicken Cat Dog

When I put into csv and put the commas:

Fox, Chicken, Cat, Dog

What I get back for output:

mylist.values.tolist()

[['Fox, nan'], ['Chicken, nan'], ['Cat, nan'], ['Dog, nan']]

I just want:

['Fox','Chicken', 'Cat', 'Dog']

Further, if I try to assign my_list = mylist.values.tolist() I get an exception the unsupported operands 'str' and 'type'.

Any help would be appreciated since I like pandas more than builtin, if it's not possible elegantly I'll have to go back.

5
  • Are you sure your output is exactly as you typed it? Is it not [['Fox', nan], ['Chicken', nan], ['Cat', nan], ['Dog', nan]]? Commented Dec 2, 2016 at 2:54
  • what type is mylist? the exception in the assignment is strange, how could I duplicate it? please, put a small minimal but complete example of the exception. Commented Dec 2, 2016 at 2:55
  • You are correct, it's a couple hundred mac addresses so I didn't copy and paste, sorry. Commented Dec 2, 2016 at 2:56
  • @eguaio >>> type(mylist) <class 'pandas.core.frame.DataFrame'> I can add code but it's really just a list copied into excel columns and read with pandas. Commented Dec 2, 2016 at 3:01
  • @Fallacy11 what columns the dataframe has? are they named? Commented Dec 2, 2016 at 3:09

3 Answers 3

1

If your output is [['Fox', nan], ['Chicken', nan], ['Cat', nan], ['Dog', nan]] (which I believe it is), then you can unzip the list into two parts:

names,nans=zip(*mylist.values.tolist()) 
# both names and nuns are tuples
names = list(names)
Sign up to request clarification or add additional context in comments.

1 Comment

I would agree it is a little strange, but the tolist() method really returns a list of strings encoding all the values separated by comma, as stated in the question. This will not work.
0

try:

slist = mylist.values.tolist()
ilist = [l[0].split(',')[0].strip() for l in slist] 

Comments

0

The formatting from the copy-pasting is somehow causing pandas to think you've given it a (4 x 2) table. So in this particular case, if your dataframe is mylist, you'll want mylist.iloc[:,0].values.tolist().

But really, just reading the file into a string and calling .split() is much simpler, faster and more robust.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.