DataFrame create data

DataFrame create data

Required packages / libraries:

import pandas as pd
import numpy as np
from pandas import DataFrame

1, using a two-dimensional array creation

df1=DataFrame(np.random.randint(0,10,(4,4)),index=[1,2,3,4],columns=['a','b','c','d'])
print(df1)
'''
创建了一个4行4列由0-10随机整数组成的二维数组
列名为a、b、c、d
索引为:1、2、3、4
'''

The output is:
Here Insert Picture Description

2, using the dictionary creation

dict={
    'province':['Guangdong','Beijing','Neimenggu','Fujian'],
    'pop':[1.3, 2.4, 1.1, 0.7],
    'year':[2018,2018,2018,2018]}
df2=pd.DataFrame(dict,index=[1,2,3,4])
print(df2)
'''
行索引(由index决定):1、2、3、4
列索引(列名):由字典中的key决定。此处就是:province、pop、year
'''

The output is:
Here Insert Picture Description
if the index of the same case, the same index value will correspond to the missing values added NaN (null value)
, for example:

data={
    'Name':pd.Series(['zs','ls','we'],index=['a','b','c']),
    'Age':pd.Series(['10','20','30','40'],index=['a','b','c','d']),
    'country':pd.Series(['中国','日本','韩国'],index=['a','c','b'])
}
df=pd.DataFrame(data)
print(df)

Above will have the same index corresponding to output results are as follows:
Here Insert Picture Description

3, using from_dict

dict2={"a":[1,2,3],"b":[4,5,6]}
df3=pd.DataFrame.from_dict(dict2)
print(df3)
'''
a和b为列名(列索引)
索引为默认索引
'''

The output is:
Here Insert Picture Description

Released five original articles · won praise 2 · views 93

Guess you like

Origin blog.csdn.net/qq_39783601/article/details/104408742