原文鏈接:https://blog.csdn.net/linchunmian/article/details/80293251
在使用 pandas 的 DataFrame 方法時碰到的一個錯誤 ValueError: If using all scalar values, you must pass an index。
這是因為 pandas 的 DataFrame 方法需要傳入一個可迭代的對象(列表,元組,字典等), 或者給 DataFrame 指定 index 參數就可以解決這個問題。如下:

解決辦法:


一、關於DataFrame 的介紹
DataFrame是Python中Pandas庫中的一種數據結構,它類似excel,是一種二維表;DataFrame的單元格可以存放數值、字符串等,這和excel表很像,同時DataFrame可以設置列名columns與行名index。
二、創建DataFrame
1.1函數創建
pandas常與numpy庫一起使用,所以通常會一起引用
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.random.randn(3, 3), index=list('abc'), columns=list('ABC'))
print(df1)
A B C
a 0.261756 1.955353 -0.412198
b 0.689325 0.177569 -1.530981
c -0.149398 -2.810199 -0.621954
1.2直接創建
df4 = pd.DataFrame([[1, 2, 3],
[2, 3, 4],
[3, 4, 5]],
index=list('abc'), columns=list('ABC'))
print(df4)
# A B C
# a 1 2 3
# b 2 3 4
# c 3 4 5
1.3字典創建
dic1 = {
'name': [
'張三', '李四', '王二麻子', '小淘氣'], 'age': [
37, 30, 50, 16], 'gender': [
'男', '男', '男', '女']}
df5 = pd.DataFrame(dic1)
print(df5)
name age gender
0 張三 37 男
1 李四 30 男
2 王二麻子 50 男
3 小淘氣 16 女
二、DataFrame屬性
2.1 查看列的數據類型
print(df5.dtypes) # age int64 # gender object # name object # dtype: object
2.2 查看DataFrame的頭尾
使用head可以查看前幾行的數據,默認的是前5行,不過也可以自己設置。
使用tail可以查看后幾行的數據,默認也是5行,參數可以自己設置。
比如看前5行。
import pandas as pd
import numpy as np
df6 = pd.DataFrame(np.arange(36).reshape(6, 6), index=list('abcdef'), columns=list('ABCDEF'))
print(df6)
# A B C D E F
# a 0 1 2 3 4 5
# b 6 7 8 9 10 11
# c 12 13 14 15 16 17
# d 18 19 20 21 22 23
# e 24 25 26 27 28 29
# f 30 31 32 33 34 35
print(df6.head())
# A B C D E F
# a 0 1 2 3 4 5
# b 6 7 8 9 10 11
# c 12 13 14 15 16 17
# d 18 19 20 21 22 23
# e 24 25 26 27 28 29
比如只看前2行
print(df6.head(2)) # A B C D E F # a 0 1 2 3 4 5 # b 6 7 8 9 10 11
比如看后5行。 print(df6.tail()) # A B C D E F # b 6 7 8 9 10 11 # c 12 13 14 15 16 17 # d 18 19 20 21 22 23 # e 24 25 26 27 28 29 # f 30 31 32 33 34 35 比如只看后2行。 print(df6.tail(2)) # A B C D E F # e 24 25 26 27 28 29 # f 30 31 32 33 34 35
2.3 查看行名與列名
print(df6.index) print(df6.columns) # Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object') # Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')
2.4 查看數據值
使用values可以查看DataFrame里的數據值,返回的是一個數組。 print(df6.values) # [[ 0 1 2 3 4 5] # [ 6 7 8 9 10 11] # [12 13 14 15 16 17] # [18 19 20 21 22 23] # [24 25 26 27 28 29] # [30 31 32 33 34 35]] 比如說查看某一列所有的數據值。 print(df6['B'].values) [ 1 7 13 19 25 31]
2.5 查看行列數
使用shape查看行列數,參數為0表示查看行數,參數為1表示查看列數 print(df6.shape[0]) print(df6.shape[1]) # 6 # 6
2.6 切片與索引
使用冒號進行切片 print(df6['a':'b']) # A B C D E F # a 0 1 2 3 4 5 # b 6 7 8 9 10 11
print(df6.loc[:,'A':'B']) # A B # a 0 1 # b 6 7 # c 12 13 # d 18 19 # e 24 25 # f 30 31 #切片表示的是行切片 #索引表示的是列索引
3 DataFrame操作
3.1 轉置
print(df6.T) # a b c d e f # A 0 6 12 18 24 30 # B 1 7 13 19 25 31 # C 2 8 14 20 26 32 # D 3 9 15 21 27 33 # E 4 10 16 22 28 34 # F 5 11 17 23 29 35
