Provide a list or numpy array
The column and row labels will simply use the numerical index
import pandas as pd
import numpy as np
z = np.array([[1,2,3,4,5],[6,7,8,9,10]])
z
pd.DataFrame(z) #note the difference to a numpy array z above
my_list = [['a', 'b', 'c'], [10,5,2.5], [3,2,1]]
print(my_list)
df = pd.DataFrame(my_list)
df #note the output here
[['a', 'b', 'c'], [10, 5, 2.5], [3, 2, 1]]
(3, 3)
Provide a dictionary
dictionary = {'a':[10,3], 'b':[5,2], 'c':[2.5,1]}
df = pd.DataFrame(dictionary)
df #note the difference with the prior dataframe you made
df.shape #note the new shape
(2, 3)
df.columns #this is how to get a list of the column headers
Index(['a', 'b', 'c'], dtype='object')
dictionary = {'a':{'row1':3, 'row2':2}, 'b':{'row1':5,'row2':2}, 'c':{'row1':2.5,'row2':1}}
df = pd.DataFrame(dictionary)
df #note the new index!
df.index #this is how you get a list of the row labels
Index(['row1', 'row2'], dtype='object')
dictionary2 = {'a':{'row1':3, 'row2':2}, 'b':{'row3':5,'row4':2}, 'c':{'row5':2.5,'row6':1}}
df2 = pd.DataFrame(dictionary2)
df2
Read a CSV file
#pandas has some great methods to read .csv files
pd.read_csv?
ms=pd.read_csv("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\mass_spec.csv")
ms
Read an Excel file
#pandas has read_excel method to read excel files
pd.read_excel?
excelf=pd.read_excel("C:\\Users\duan\Desktop\PythonDataProcessingVisualization\excelfile.csv")
excelf