将列表转换为Pandas数据框列


80

我需要将列表转换为一列熊猫数据框

当前列表(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中的索引。


1
您的代码(和错误)值得投入了,这样我们就可以解释你做错了什么
OneCricketeer

Answers:


159

使用:

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

15
df = pd.DataFrame(L)
DYZ '02

@jezrael嗨,如何设置与逗号不同的分隔符?
sygneto

@sygneto-您需要L = stringdata.split(';')吗?
jezrael19年

@jezarael我正在尝试使用split然后将其转换为AttributeError: 'list' object has no attribute 'split'
dataframe,

1
@GuillaumeLabs-是的,如果使用了list像变量之类的东西,例如list=[1,2,3],您需要list1L,然后重新启动IDE并一切正常,就会发生这种情况。
jezrael

11

如果您的列表如下所示:[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

3

您可以直接致电

pd.DataFrame()

方法,并将您的列表作为参数传递。

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)),对吧?
阿迪尔·阿巴吉尔(Adir Abargil)

2

例:

['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

希望这可以帮助!!


0

为了将列表转换成Pandas核心数据框架,我们需要使用pandas包中的DataFrame方法。

有多种执行上述操作的方法。

将熊猫作为pd导入

  1. pd.DataFrame({'Column_Name':Column_Data})
  • Column_Name:字符串
  • Column_Data:列表形式
  1. 数据= 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})
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.