0
点赞
收藏
分享

微信扫一扫

将表格的列标题作为第一行, 转为二维list

天悦哥 2023-11-30 阅读 23

# 将表格的列标题作为第一行, 转为二维list
# 情况1_1, 表格, 无数据; 情况1_2, 表格, 有数据
data = [[1,1]]
columns = ['col1', 'col2']
df = pd.DataFrame(data=data, columns=columns)
df_concat = pd.concat([
    # to_frame(index: 'bool' = True, name: 'Hashable' = <no_default>)
    df.columns.to_frame().T,
    df
]).reset_index(drop=True)
display(df_concat)

# 情况2_1, series, 无列名; 情况2_2, series, 有列名
data = [1, 1]
name = 'ser_name'
ser = pd.Series(data, name=name)
# futurewarning None dtype will be object instead of now float64
ser_name = pd.Series(ser.name)
pd.concat([ser_name, ser])

举报

相关推荐

0 条评论