简体   繁体   English

将列表转换为 Pandas Dataframe 列

[英]Convert List to Pandas Dataframe Column

I need to convert my list into a one-column pandas dataframe.我需要将我的列表转换为单列 pandas dataframe。

Current List (len=3):当前列表(len=3):

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

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

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

NB The numbers represent index in the required Pandas DF above.注意:数字代表上面所需的 Pandas DF 中的索引。

Use:用:

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

Thank you DYZ :谢谢DYZ :

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

if your list looks like this: [1,2,3] you can do:如果您的列表如下所示:[1,2,3] 您可以:

import pandas as pd

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

to get this:得到这个:

    col1    col2    col3
0   1       2       3

alternatively you can create a column as follows:或者,您可以按如下方式创建列:

import numpy as np
import pandas as pd

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

to get this:得到这个:

  col1
0   1
1   2
2   3

You can directly call the你可以直接调用

pd.DataFrame() pd.DataFrame()

method and pass your list as parameter.方法并将您的列表作为参数传递。

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

Output:输出:

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

And if you have multiple lists and you want to make a dataframe out of it.You can do it as following:如果您有多个列表,并且想从中制作一个数据框。您可以按以下方式进行:

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()

Output:输出:

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

Example:例子:

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

code block:代码块:

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

Output:输出:

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

It is not recommended to remove the column names of the panda dataframe.不建议删除熊猫数据框的列名。 but if you still want your data frame without header(as per the format you posted in the question) you can do this:但是,如果您仍然想要没有标题的数据框(按照您在问题中发布的格式),您可以这样做:

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

Output will be like this:输出将是这样的:

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

or或者

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

But the output will be a list instead of a dataframe:但输出将是一个列表而不是一个数据框:

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

Hope this helps!!希望这可以帮助!!

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

Column name:栏目名称:

df.columns = ['col name']

For Converting a List into Pandas Core Data Frame, we need to use DataFrame Method from pandas Package.对于将列表转换大熊猫核心数据帧,我们需要从熊猫包用数据帧的方法。

There are Different Ways to Perform the Above Operation.有不同的方法来执行上述操作。

import pandas as pd将熊猫导入为 pd

  1. pd.DataFrame({'Column_Name':Column_Data}) pd.DataFrame({'Column_Name':Column_Data})
  • Column_Name : String列名称:字符串
  • Column_Data : List Form Column_Data :列表表单
  1. Data = pd.DataFrame(Column_Data)数据 = pd.DataFrame(Column_Data)

    Data.columns = ['Column_Name'] Data.columns = ['Column_Name']

So, for the above mentioned issue, the code snippet is所以,对于上面提到的问题,代码片段是

import pandas as pd

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

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

You can also assign() a list to an existing dataframe. This is especially useful if you're chaining multiple methods and you need to assign a column that you need to use later in the chain.您还可以assign()一个列表到现有的 dataframe。如果您链接多个方法并且您需要分配一个您需要在链中稍后使用的列,这将特别有用。

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

资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM