Federico Gentile Federico Gentile - 4 years ago 115
Python Question

How to save lists within rows with Pandas

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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download