import pandas as pd
Create a DataFrame from a dictionary, containing two columns: numbers and colors. Each key represent a column name and the value is a series of data, the content of the column:
df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})
Show contents of d...
Create a DataFrame of random numbers:
import numpy as np
import pandas as pd
# Set the seed for a reproducible sample
np.random.seed(0)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
print(df)
# Output:
# A B C
# 0 1.764052 0.400157 0.9787...
You can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples you want to use. Here we will create a DataFrame using all of the data in each tuple except for the last element.
import pandas as pd
data = [
('p1', 't1', 1, 2),
('p1', 't2', 3, 4)...
Create a DataFrame from multiple lists by passing a dict whose values lists. The keys of the dictionary are used as column labels. The lists can also be ndarrays. The lists/ndarrays must all be the same length.
import pandas as pd
# Create DF from dict of lists/ndarrays
df = pd.DataFrame({'...
import pandas as pd
# Save dataframe to pickled pandas object
df.to_pickle(file_name) # where to save it usually as a .plk
# Load dataframe from pickled pandas object
df= pd.read_pickle(file_name)
A DataFrame can be created from a list of dictionaries. Keys are used as column names.
import pandas as pd
L = [{'Name': 'John', 'Last Name': 'Smith'},
{'Name': 'Mary', 'Last Name': 'Wood'}]
pd.DataFrame(L)
# Output: Last Name Name
# 0 Smith John
# 1 Wood Mary
Missin...