2

I have a dataframe df1 and each row of column A contains a list of numbers like so:

import pandas as pd
dic = {'A':[[0,2,5,464,64789,131,13],[1,454,45166,78],[6,8],[1],[7,8789,45,65]]}
df1 = pd.DataFrame(dic)

My goal is to save each row in a .txt file so that it displays for each row the content of the list. So far I was able to achieve it by using the following line of code:

df1.to_csv('../ndb/_fede.txt',index=False, header=None)

However the output file has some quotation marks and brackets which I don't need.

# What I get                       | # What I WANT!
"[0, 2, 5, 464, 64789, 131, 13]"   | 0,2,5,464,64789,131,13
"[1, 454, 45166, 78]"              | 1,454,45166,78
"[6, 8]"                           | 6,8
"[1]"                              | 1
"[7, 8789, 45, 65]"                | 7,8789,45,65

Basically what I want is that all numbers of each list are separated by a comma and no other characters.

Note: you can also take the df1 converted into arrays if needed.

2 Answers 2

4

The simplier solution is cast to str and remove [] by split:

df1.A = df1.A.astype(str).str.strip('[]')
print (df1)
                              A
0  0, 2, 5, 464, 64789, 131, 13
1             1, 454, 45166, 78
2                          6, 8
3                             1
4               7, 8789, 45, 65

And if need remove whitespaces:

df1.A = df1.A.astype(str).str.strip('[]').str.replace('\s+', '')
print (df1)
                        A
0  0,2,5,464,64789,131,13
1          1,454,45166,78
2                     6,8
3                       1
4            7,8789,45,65

If need save only one column the simpliest is change default separator ,, because separator , and values in column are , , so " are added:

print (df1.to_csv(index=False, header=None, sep="|"))
0,2,5,464,64789,131,13
1,454,45166,78
6,8
1
7,8789,45,65
Sign up to request clarification or add additional context in comments.

2 Comments

I like this but when I save it I still get the quotation marks
If it is only one column, try some sep onother as , - df1.to_csv('../ndb/_fede.txt',index=False, header=None, sep='|')
1
df1.A.apply(
    lambda x: ','.join(pd.Series(x).astype(str))
).to_frame().to_csv('../ndb/_fede.txt', index=False, sep='|')

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.