I have 10 lists with thousands of rows, for example:
l1 = ['a1', 'a2', ...], l2 = ['1', '2', ...], ..., l10 = ['abc', 'sde',...]
the count of rows of all of them is the same. I would like to create a CSV file like:
name reg... address
'a1' '1'... 'abc'
'a2' '2'... 'sde'
First I thought using pandas DataFrame:(I only used 103 first rows for testing)
data = [l1, l2,..., l10]
lables = ['name', 'reg', ...,'address']
df = pd.DataFrame(data, columns=labels)
....
I got this error:
Traceback (most recent call last): File "ch.py", line 122, in status_list, retrieved_at_list, source_url_list) File "ch.py", line 95, in charity df = pd.DataFrame(data, columns=labels) File "C:\Users\MON\AppData\Local\Programs\Python\Python36-32\lib\site packages\ pandas\core\frame.py", line 369, in init arrays, columns = _to_arrays(data, columns, dtype=dtype) File "C:\Users\MON\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ pandas\core\frame.py", line 6284, in _to_arrays dtype=dtype) File "C:\Users\MON\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ pandas\core\frame.py", line 6363, in _list_to_arrays coerce_float=coerce_float) File "C:\Users\MON\AppData\Local\Programs\Python\Python36-32\lib\site-packages\ pandas\core\frame.py", line 6420, in _convert_object_array 'columns' % (len(columns), len(content)))
AssertionError: 10 columns passed, passed data had 103 columns
Then I tried to use:
data = [l1, l2,..., l10]
with open('charity.csv', 'w') as ch_list:
wr = csv.writer(ch_list, lineterminator='\n')
wr.writerows(data)
But I got all of the data of l1 to l10 in one column.
I have two questions:
1- How can I solve my problem? In terms of performance, I prefer to use pandas DataFrame, however, I am open to any new suggestions.
2-What is the meaning of the error I got for DataFrame and how can I solve it?