import numpy as np
import pandas as pd
for obj in ['StrOrIntOr', [], ['element'], [[]], [[],[]]]:
arr = np.array(obj)
df = pd.DataFrame(
[i, type(i)]
for i in
[obj, arr, arr.shape, len(arr.shape)]
)
df.columns = ["display", "type"]
df.index = ["obj", "array", "shape", "dimmension"]
display(df)
import numpy as np
import pandas as pd
for obj in ['StrOrIntOr', [], ['element'], [[]], [[],[]]]:
arr = np.array(obj)
dim = len(arr.shape)
if dim == 0:
# 需要[]
df = pd.DataFrame([arr])
# df.shape[0] == 1, 可取行下标
df_setFirstRowAsColName = df.copy()
df_setFirstRowAsColName.columns = df.iloc[0]
df_dropFirstRow = df_setFirstRowAsColName.drop(index=0).reset_index(drop=True)
elif dim == 1:
df = pd.DataFrame(arr)
# np.array([]).shape == (0,)
# pd.DataFrame(np.array([])).shape == (0, 1)
# 不可取行下标
if arr.shape[0] == 0:
df_setFirstRowAsColName = df.copy()
df_setFirstRowAsColName.columns = ["不可取下标"]
df_dropFirstRow = "不可取下标"
# array(['element']).shape = (1,)
elif arr.shape[0] > 0:
df_setFirstRowAsColName = df.copy()
df_setFirstRowAsColName.columns = df.iloc[0]
df_dropFirstRow = df_setFirstRowAsColName.drop(index=0).reset_index(drop=True)
elif dim > 1:
df = pd.DataFrame(arr)
df_setFirstRowAsColName = df.copy()
df_setFirstRowAsColName.columns = df.iloc[0]
df_dropFirstRow = df_setFirstRowAsColName.drop(index=0).reset_index(drop=True)
标签:index,arr,下标,df,shape,setFirstRowAsColName,array
From: https://blog.51cto.com/u_16055028/8840940