Python:根据列表创建Pandas数据框

ef1yzkbh  于 2022-11-26  发布在  Python
关注(0)|答案(5)|浏览(156)

我使用以下代码从列表创建数据框:

test_list = ['a','b','c','d']
df_test = pd.DataFrame.from_records(test_list, columns=['my_letters'])
df_test

上面的代码运行良好。然后我对另一个列表尝试了同样的方法:

import pandas as pd
q_list = ['112354401', '116115526', '114909312', '122425491', '131957025', '111373473']
df1 = pd.DataFrame.from_records(q_list, columns=['q_data'])
df1

但这一次它给了我以下错误:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-24-99e7b8e32a52> in <module>()
      1 import pandas as pd
      2 q_list = ['112354401', '116115526', '114909312', '122425491', '131957025', '111373473']
----> 3 df1 = pd.DataFrame.from_records(q_list, columns=['q_data'])
      4 df1

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in from_records(cls, data, index, exclude, columns, coerce_float, nrows)
   1021         else:
   1022             arrays, arr_columns = _to_arrays(data, columns,
-> 1023                                              coerce_float=coerce_float)
   1024 
   1025             arr_columns = _ensure_index(arr_columns)

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _to_arrays(data, columns, coerce_float, dtype)
   5550         data = lmap(tuple, data)
   5551         return _list_to_arrays(data, columns, coerce_float=coerce_float,
-> 5552                                dtype=dtype)
   5553 
   5554 

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _list_to_arrays(data, columns, coerce_float, dtype)
   5607         content = list(lib.to_object_array(data).T)
   5608     return _convert_object_array(content, columns, dtype=dtype,
-> 5609                                  coerce_float=coerce_float)
   5610 
   5611 

/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py in _convert_object_array(content, columns, coerce_float, dtype)
   5666             # caller's responsibility to check for this...
   5667             raise AssertionError('%d columns passed, passed data had %s '
-> 5668                                  'columns' % (len(columns), len(content)))
   5669 
   5670     # provide soft conversion of object dtypes

AssertionError: 1 columns passed, passed data had 9 columns

为什么同样的方法只适用于一个列表,而不适用于另一个列表?知道这里可能出了什么问题吗?非常感谢!

ne5o7dgx

ne5o7dgx1#

DataFrame.from_records将字符串视为字符列表。因此它需要与字符串长度一样多的列。
您可以简单地使用the DataFrame constructor

In [3]: pd.DataFrame(q_list, columns=['q_data'])
Out[3]:
      q_data
0  112354401
1  116115526
2  114909312
3  122425491
4  131957025
5  111373473
2cmtqfgy

2cmtqfgy2#

In[20]: test_list = [['a','b','c'], ['AA','BB','CC']]

In[21]: pd.DataFrame(test_list, columns=['col_A', 'col_B', 'col_C'])
Out[21]: 
  col_A col_B col_C
0     a     b     c
1    AA    BB    CC

In[22]: pd.DataFrame(test_list, index=['col_low', 'col_up']).T
Out[22]: 
  col_low col_up
0       a     AA
1       b     BB
2       c     CC
5gfr0r5j

5gfr0r5j3#

如果你想从多个列表中创建一个DataFrame,你可以简单地压缩列表。这将返回一个“zip”对象。所以你将转换回一个列表。

mydf = pd.DataFrame(list(zip(lstA, lstB)), columns = ['My List A', 'My List B'])
sc4hvdpw

sc4hvdpw4#

你也可以借助numpy。

import numpy as np
df1 = pd.DataFrame(np.array(q_list),columns=['q_data'])
pvabu6sv

pvabu6sv5#

只使用concat方法

test_list = ['a','b','c','d']
pd.concat(test_list )

相关问题