pandas创建DataFrame

创建空的DataFrame

df=pd.DataFrame(columns=['A','B','C'],index=[0,1,2])

结果:

	A	B	C
0	NaN	NaN	NaN
1	NaN	NaN	NaN
2	NaN	NaN	NaN

手动创建DataFrame

df=pd.DataFrame(data=[['Apple',5],
                      ['Banana',10],
                      ['Cherry',8],
                      ['Dates',3],
                      ['Eggfruit',7]],
                columns = ['Fruits','Quantity'],
                index=[1,2,3,4,5])

结果:

	Fruits	    Quantity
1	Apple	    5
2	Banana	    10
3	Cherry	    8
4	Dates	    3
5	Eggfruit	7

使用列表创建DataFrame

fruits_list=['Apple','Banana','Cherry','Dates','Eggfruits']
df=pd.DataFrame(fruits_list,
                columns = ['Fruits'],
                index=['a','b','c','d','e'])

结果:

	Fruits
a	Apple
b	Banana
c	Cherry
d	Dates
e	Eggfruits

使用字典创建DataFrame

fruits_dict = { 'Fruits':['Apple','Banana','Cherry','Dates','Eggfruit'],
                'Quantity': [5, 10, 8, 3, 7],  
                'Color': ['Red', 'Yellow', 'Red', 'Brown', 'Yellow']}
df=pd.DataFrame(fruits_dict)

结果:

	Fruits	Quantity	Color
0	Apple	5	Red
1	Banana	10	Yellow
2	Cherry	8	Red
3	Dates	3	Brown
4	Eggfruit	7	Yellow

利用csv文件进行创建

fruits = pd.read_csv('fruits.csv')

你可能感兴趣的:(pandas,pandas,python)