将列表转换为Pandas数据框列

2024-04-19 01:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将我的列表转换为一列pandas数据框

当前列表(len=3):

['Thanks You',
 'Its fine no problem',
 'Are you sure']

所需熊猫DF(形状=3,):

0 Thank You
1 Its fine no problem
2 Are you sure

请注意,数字代表上述所需熊猫DF中的索引。


Tags: 数据noyoupandasdf列表lenare
2条回答

如果你的列表是这样的:[1,2,3]你可以:

lst = [1,2,3]
df = pd.DataFrame([lst])
df.columns =['col1','col2','col3']
df

为了得到这个:

    col1    col2    col3
0   1       2       3

或者,也可以创建列,如下所示:

import numpy as np
df = pd.DataFrame(np.array([lst]).T)
df.columns =['col1']
df

为了得到这个:

  col1
0   1
1   2
2   3

使用:

L = ['Thanks You', 'Its fine no problem', 'Are you sure']

#create new df 
df = pd.DataFrame({'col':L})
print (df)

                   col
0           Thanks You
1  Its fine no problem
2         Are you sure

df = pd.DataFrame({'oldcol':[1,2,3]})

#add column to existing df 
df['col'] = L
print (df)
   oldcol                  col
0       1           Thanks You
1       2  Its fine no problem
2       3         Are you sure

谢谢DYZ

#default column name 0
df = pd.DataFrame(L)
print (df)
                     0
0           Thanks You
1  Its fine no problem
2         Are you sure

相关问题 更多 >