DataFrame is a tabular data structure that can be thought of as a table in excel.
Official Documentation:/docs/reference/
DataFrame Creation
The DataFrame constructor is as follows:
(data=None, index=None, columns=None, dtype=None, copy=False)
- data: The data portion of the DataFrame, which can be a dictionary, two-dimensional array, Series, DataFrame, or other object that can be converted to a DataFrame; if this parameter is not provided, an empty DataFrame is created.
- index: The row index of DataFrame, used to identify each row of data, can be a list, array, index object, etc. If this parameter is not provided, a default integer index is created.
- columns: The column index of the DataFrame, used to identify each column of data. It can be a list, array, index object, etc. If this parameter is not provided, a default integer index will be created.
- dtype: Specify the data type of DataFrame, it can be the data type of NumPy, such as np.int64, np.float64, etc. If this parameter is not provided, the data type will be inferred automatically according to the data.
- copy: Whether to copy the data, the default is False, which means no data is copied, if set to True, the input data is copied.
One-dimensional list creation DataFrame
|
DataFrame creation for 2D lists
|
Pass a dictionary to create a DataFrame
|
Pass a list of dictionaries to create a DataFrame
|
Created by Series object
|
Created via Numpy
|
Properties of a DataFrame
attribute is mainly used to transpose rows and columns, which is the same as what () accomplishes.
|
Returns a list of row and column indices, which can be converted to a row-indexed list by [0].tolist() or list([0]), and a column-indexed list by [0].tolist() or list([0]).
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
# on the basis ofaArray CreationDataFrame
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # [Index(['a', 'b', 'c'], dtype='object'), Index(['foo', 'bar'], dtype='object')]
print([0].tolist()) # ['a', 'b', 'c']
print(list([0])) # ['a', 'b', 'c']
View the data type of each column.
|
Gets the number of dimensions of the DataFrame.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
# on the basis ofaArray CreationDataFrame
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # 2
Gets the number of rows and columns of the DataFrame, which is a tuple.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
# on the basis ofaArray CreationDataFrame
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # (3, 2)
Returns the number of elements in the DataFrame.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
# on the basis ofaArray creationDataFrame
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # 6
Returns a two-dimensional array of all rows of data, where each element is a one-dimensional array (i.e., one row of data) that can be passed through thelist()
maybe()
Convert to a python list type.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
# on the basis ofaArray creationDataFrame
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # [[8 6] [3 3] [8 7]]
print(list()) # [array([8, 6], dtype=int32), array([3, 3], dtype=int32), array([8, 7], dtype=int32)]
print(()) # [[8, 6], [3, 3], [8, 7]]
Gets the row index, which is returned as Index. list()
maybe()
Convert to a list.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # Index(['a', 'b', 'c'], dtype='object')
print() # ['a' 'b' 'c']
print(list()) # ['a', 'b', 'c']
print(()) # ['a', 'b', 'c']
Gets the column index, which is returned as type Index, and can be passed through thelist()
maybe()
Convert to a list.
import pandas as pd
import numpy as np
a = (1, 10, (3, 2))
df = (a, columns=['foo', 'bar'], index=['a', 'b', 'c'])
print(df)
print("*" * 50)
print() # Index(['foo', 'bar'], dtype='object')
print() # ['foo' 'bar'],usability () Convert to list
print(list()) # ['foo', 'bar']
print(()) # ['foo', 'bar']