Data Frame Creation -- python pandas¶
In [1]:
Copied!
import pandas as pd
import pandas as pd
From columns¶
In [9]:
Copied!
# From lists/arrays of columns
pd.DataFrame.from_dict({
'x': [1,2,3],
'y': ['a', 'b', 'c'],
'z': [True, False, True]
})
# From lists/arrays of columns
pd.DataFrame.from_dict({
'x': [1,2,3],
'y': ['a', 'b', 'c'],
'z': [True, False, True]
})
Out[9]:
| x | y | z | |
|---|---|---|---|
| 0 | 1 | a | True |
| 1 | 2 | b | False |
| 2 | 3 | c | True |
From rows¶
In [12]:
Copied!
# From list/array of rows, 2D array
pd.DataFrame.from_records([
[1, 'a', True],
[2, 'b', False],
[3, 'c', True],
], columns=['x', 'y', 'z'])
# From list/array of rows, 2D array
pd.DataFrame.from_records([
[1, 'a', True],
[2, 'b', False],
[3, 'c', True],
], columns=['x', 'y', 'z'])
Out[12]:
| x | y | z | |
|---|---|---|---|
| 0 | 1 | a | True |
| 1 | 2 | b | False |
| 2 | 3 | c | True |
In [13]:
Copied!
# From list of dict rows with column name keys
# File is jsonl / ndjson
pd.DataFrame.from_records([
{'x': 1, 'y': 'a', 'z': True},
{'x': 2, 'y': 'b', 'z': False},
{'x': 3, 'y': 'c', 'z': True},
])
# From list of dict rows with column name keys
# File is jsonl / ndjson
pd.DataFrame.from_records([
{'x': 1, 'y': 'a', 'z': True},
{'x': 2, 'y': 'b', 'z': False},
{'x': 3, 'y': 'c', 'z': True},
])
Out[13]:
| x | y | z | |
|---|---|---|---|
| 0 | 1 | a | True |
| 1 | 2 | b | False |
| 2 | 3 | c | True |
In [6]:
Copied!
# From dict rows with column name keys
pd.DataFrame.from_dict({
'i': {'x': 1, 'y': 'a', 'z': True},
'ii': {'x': 2, 'y': 'b', 'z': False},
'iii': {'x': 3, 'y': 'c', 'z': True},
}, orient='index')
# From dict rows with column name keys
pd.DataFrame.from_dict({
'i': {'x': 1, 'y': 'a', 'z': True},
'ii': {'x': 2, 'y': 'b', 'z': False},
'iii': {'x': 3, 'y': 'c', 'z': True},
}, orient='index')
Out[6]:
| x | y | z | |
|---|---|---|---|
| i | 1 | a | True |
| ii | 2 | b | False |
| iii | 3 | c | True |
In [ ]:
Copied!