DataFrame creation program & use dictionary to create dataframe object

import numpy as np
import pandas as pd

DataFrame creator

Create row and column index

arr1 = np.arange(10).reshape(2,5)
arr1
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
demo1 = pd.DataFrame(arr1,index=['a','b'],columns=['A','B','C','D','E'])
demo1
A B C D E
a 0 1 2 3 4
b 5 6 7 8 9

Use the dictionary to create a dataframe object

dict1 = {
    
    'a':[0.1,2.3],'b':[2.3,4.3],'c':[3.4,5.9]}
dict1
{'a': [0.1, 2.3], 'b': [2.3, 4.3], 'c': [3.4, 5.9]}
demo2 = pd.DataFrame(dict1)
demo2
a b c
0 0.1 2.3 3.4
1 2.3 4.3 5.9
demo2.values.dtype
dtype('float64')

Add and delete dataframe objects

df1 = pd.DataFrame(np.arange(9).reshape(3,3),columns=['A','B','C'])
df1
A B C
0 0 1 2
1 3 4 5
2 6 7 8

Add a column

df1['D']=[1,2,3]
df1
A B C D
0 0 1 2 1
1 3 4 5 2
2 6 7 8 3

Delete a column

del df1['C']
df1
A B D
0 0 1 1
1 3 4 2
2 6 7 3

Guess you like

Origin blog.csdn.net/m0_46202060/article/details/115082259