我需要将列表转换为一列熊猫数据框
当前列表(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中的索引。
Answers:
使用:
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
df = pd.DataFrame(L)
L = stringdata.split(';')
吗?
AttributeError: 'list' object has no attribute 'split'
list
像变量之类的东西,例如list=[1,2,3]
,您需要list1
或L
,然后重新启动IDE并一切正常,就会发生这种情况。
您可以直接致电
方法,并将您的列表作为参数传递。
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
并且如果您有多个列表,并且想要从中创建一个数据框,则可以执行以下操作:
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
pd.DataFrame(zip(names,salary,age))
,对吧?
例:
['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
不建议删除熊猫数据框的列名。但是,如果您仍然希望数据框不包含标题(按照问题中发布的格式),则可以执行以下操作:
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)
但是输出将是列表而不是数据框:
0 Thanks You
1 Its fine no problem
2 Are you sure
希望这可以帮助!!
为了将列表转换成Pandas核心数据框架,我们需要使用pandas包中的DataFrame方法。
有多种执行上述操作的方法。
将熊猫作为pd导入
数据= pd.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})