将列表转换为Pandas数据框架列

llycmphe  于 2022-12-16  发布在  其他
关注(0)|答案(7)|浏览(213)

我需要把我的列表转换成一列Pandas数据框。
当前列表(len=3):

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

所需Pandas DF(形状=3,):

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

注意:数字代表上述所需Pandas DF中的指数。

bfhwhh0e

bfhwhh0e1#

用途:

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
ars1skjm

ars1skjm2#

如果您的列表如下所示:[1,2,3]您可以:

import pandas as pd

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
import pandas as pd

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

得到这个:

col1
0   1
1   2
2   3
rjee0c15

rjee0c153#

您可以直接调用pd.DataFrame()方法并将列表作为参数传递。

import pandas as pd
l = ['Thanks You','Its fine no problem','Are you sure']
pd.DataFrame(l)

输出:

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

如果你有多个列表,并且你想把它们做成一个 Dataframe ,你可以这样做:

import pandas as pd
names =["A","B","C","D"]
salary =[50000,90000,41000,62000]
age = [24,24,23,25]
data = pd.DataFrame([names,salary,age]) #Each list would be added as a row
data = data.transpose() #To Transpose and make each rows as columns
data.columns=['Names','Salary','Age'] #Rename the columns
data.head()

输出:

Names   Salary  Age
0       A    50000   24
1       B    90000   24
2       C    41000   23
3       D    62000   25
qkf9rpyu

qkf9rpyu4#

示例:

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

代码块:

import pandas as pd
df = pd.DataFrame(lst)

输出:

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

不建议删除Pandas Dataframe 的列名。但如果您仍然希望 Dataframe 没有标题(按照您在问题中发布的格式),您可以这样做:

df = pd.DataFrame(lst)    
df.columns = ['']

输出将如下所示:

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

df = pd.DataFrame(lst).to_string(header=False)

但输出将是一个列表而不是 Dataframe :

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

希望这有帮助!!

mlnl4t2r

mlnl4t2r5#

要将列表转换为Pandas核心 Dataframe ,我们需要使用pandas包中的DataFrame方法。
执行上述操作有不同的方法。(假设Pandas导入为pd

  1. pandas.DataFrame({'Column_Name':Column_Data})

*列名:字符串
*列_数据:列表形式

Data = pandas.DataFrame(Column_Data)`
 Data.columns = ['Column_Name']

因此,对于上述问题,代码片段为

import pandas as pd

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

Data = pd.DataFrame({'Text': Content})
tkqqtvp1

tkqqtvp16#

你也可以将一个列表assign()到一个已有的 Dataframe 中,这在你链接多个方法并且需要分配一个列以便以后在链中使用时特别有用。

df = pd.DataFrame()
df1 = df.assign(col=['Thanks You', 'Its fine no problem', 'Are you sure'])

7uhlpewt

7uhlpewt7#

list = ['Thanks You', 'Its fine no problem', 'Are you sure']
df = pd.DataFrame(list)

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

色谱柱名称:

df.columns = ['col name']

相关问题