简体   繁体   中英

Pandas DataFrame constructor takes list input?

The pandas documentation for the DataFrame constructor lists the following argument types for the data argument: numpy ndarray (structured or homogeneous), dict, or DataFrame. Yet it clearly accepts a list of lists (see below). I'm relatively new to pandas and just running through a bunch of tests/examples. Does pandas generally accept python lists in place of ndarrays?

import pandas as pd
DF_From_List_Input = pd.DataFrame([[1,2,3],[4,5,6]])  #works fine!

Yes, Pandas generally accepts Python lists in place of ndarrays. Looking at the source code for the DataFrame class, there's an explicit check for this, currently on line 255, followed by logic for creating a DataFrame.

elif isinstance(data, (list, types.GeneratorType)):

Anecdotally, I've never run into any issues passing a list of lists to the DataFrame constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM