DataFrame创建的几种方法

python的基础知识薄弱,一些基础的东西都不太熟练,慢慢来吧

有行有列索引,可看成是series组成的字典:

image-20230515121707267

image-20230515121713722

image-20230515122035796

构造方法:

pandas.DataFrame( data, index, columns, dtype, copy)
  • data:一组数据(ndarray、series, map, lists, dict 等类型)
  • index:索引值,或者可以称为行标签
  • columns:列标签,默认为 RangeIndex (0, 1, 2, …, n)
  • dtype:数据类型
  • copy:拷贝数据,默认为 False

1、列表创建

import pandas as pd

data = [['Google',10],['Runoob',12],['Wiki',13]]

df = pd.DataFrame(data,columns=['Site','Age'],dtype=float)

print(df)

2、ndarrays创建

import pandas as pd

data = {'Site':['Google', 'Runoob', 'Wiki'], 'Age':[10, 12, 13]}

df = pd.DataFrame(data)

print (df)

创建的结果如下

image-20230515122134493

3、字典创建

import pandas as pd

data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]

df = pd.DataFrame(data)

print (df)

输出的结果为:
   a   b     c
0  1   2   NaN
1  5  10  20.0

4、使用loc返回指定行数据

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

# 数据载入到 DataFrame 对象
df = pd.DataFrame(data)

# 返回第一行
print(df.loc[0])
# 返回第二行
print(df.loc[1])

-----结果-------
calories    420
duration     50
Name: 0, dtype: int64
calories    380
duration     40
Name: 1, dtype: int64

5、返回多行数据

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

# 数据载入到 DataFrame 对象
df = pd.DataFrame(data)

# 返回第一行和第二行
print(df.loc[[0, 1]])

-----结果-------

   calories  duration
0       420        50
1       380        40

6、指定索引值

import pandas as pd

data = {
    "calories":[420,380,390]
    "duration":[50,40,45]
}

df = pd.DataFrame(data,index = ["day1","day2","day3"])

print(df)

-----结果-------

      calories  duration
day1       420        50
day2       380        40
day3       390        45

7、loc返回某一行

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}

df = pd.DataFrame(data, index = ["day1", "day2", "day3"])

# 指定索引
print(df.loc["day2"])

-----结果-------

calories    380
duration     40
Name: day2, dtype: int64

内容来源:
https://www.runoob.com/pandas/pandas-dataframe.html