Pandas DataFrame from_records() Method
In this tutorial, we will learn the Python pandas DataFrame.from_records()
method. It converts structured or record ndarray to DataFrame. It creates a DataFrame object from a structured ndarray, sequence of tuples or dicts, or DataFrame.
The below is the syntax of the DataFrame.from_records()
method.
Syntax
DataFrame.from_records(data, index=None, exclude=None, columns=None, coerce_float=False, nrows=None)
Parameters
data: structured ndarray, the sequence of tuples or dicts, or DataFrame. Structured input data.
index: str, list of fields, array-like. Field of the array to use as the index, alternately a specific set of input labels to use.
exclude: sequence, default None. Columns or fields to exclude.
columns: sequence, default None. Column names to use. If the passed data do not have names associated with them, this argument provides names for the columns. Otherwise, this argument indicates the order of the columns in the result (any names not found in the data will become all-NA columns).
coerce_float: bool, default False. Attempt to convert values of non-string, non-numeric objects (like decimal. Decimal) to floating point, useful for SQL result sets.
nrows: int, default None. A number of rows to read if data is an iterator.
Example: Create DataFrame using the DataFrame.from_records()
Method
Data can be provided as a structured ndarray. The DataFrame.from_records()
method converts ndarray record to DataFrame. See the below example.
#import pandas as pd
import pandas as pd
#import numpy as np
import numpy as np
data = np.array([(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')],dtype=[('col_1', 'i4'), ('col_2', 'U1')])
print("ndarray is:",data)
df=pd.DataFrame.from_records(data)
print("----------The DataFrame is----------")
print(df)
Once we run the program we will get the following output.
ndarray is: [(3, 'a') (2, 'b') (1, 'c') (0, 'd')]
----------The DataFrame is----------
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
Example 2: Create DataFrame using the DataFrame.from_records()
Method
Here, we are using a list of dicts. The DataFrame.from_records()
method converts the list of dicts records to DataFrame. The below example shows the same.
#import pandas as pd
import pandas as pd
#import numpy as np
import numpy as np
data = [{'col_1': 3, 'col_2': 'a'},{'col_1': 2, 'col_2': 'b'},{'col_1': 1, 'col_2': 'c'},{'col_1': 0, 'col_2': 'd'}]
print("list of dicts:",data)
df=pd.DataFrame.from_records(data)
print("----------The DataFrame is----------")
print(df)
Once we run the program we will get the following output.
list of dicts: [{'col_1': 3, 'col_2': 'a'}, {'col_1': 2, 'col_2': 'b'}, {'col_1': 1, 'col_2': 'c'}, {'col_1': 0, 'col_2': 'd'}]
----------The DataFrame is----------
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
Example 2: Create DataFrame using the DataFrame.from_records()
Method
Here, we are using as a list of tuples. The DataFrame.from_records()
method converts the list of tuples records to DataFrame. The below example shows the same.
#import pandas as pd
import pandas as pd
#import numpy as np
import numpy as np
data = [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')]
print("list of tuples:",data)
df=pd.DataFrame.from_records(data)
print("----------The DataFrame is----------")
print(df)
Once we run the program we will get the following output.
list of tuples: [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')]
----------The DataFrame is----------
0 1
0 3 a
1 2 b
2 1 c
3 0 d
Conclusion
In this tutorial, we learned the Python pandas DataFrame.from_records()
method. We learned the syntax, parameters and by applying this method on the DataFrame, understood the DataFrame.from_records()
method.