pd.DataFrame()函数

pd.DataFrame()函数

import pandas as pd
import numpy as np

data = {
   'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': [2000, 2001, 2002, 2001, np.nan],  # np.nan表示NA
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
pd.DataFrame(data,
#           index=['a','b','c','d','e']
#           index = range(5)
         )  # 默认生成整数索引, 字典的键作列,值作行

state year pop
0 Ohio 2000.0 1.5
1 Ohio 2001.0 1.7
2 Ohio 2002.0 3.6
3 Nevada 2001.0 2.4
4 Nevada NaN 2.9
# 两层嵌套
d = {
   'a': {
   'tp': 26, 'fp': 112},
     'b': {
   'tp': 26, 'fp': 91},
     'c': {
   'tp': 23, 'fp': 74}}
df_index = pd.DataFrame.from_dict(d, orient='index')
df_index

tp fp
a 26 112
b 26 91
c 23 74
df_columns = pd.DataFrame.from_dict(d,orient='columns')
df_columns

a b c
tp 26 26 23
fp 112 91 74
# 通过传递一个numpy array,时间索引以及列标签来创建一个DataFrame
data = pd.DataFrame(np.arange(10

你可能感兴趣的:(pd.DataFrame()函数)