Convert List to Pandas Dataframe Column
Question:
I need to convert my list into a one-column pandas dataframe.
Current List (len=3):
['Thanks You',
'Its fine no problem',
'Are you sure']
Required Pandas DF (shape =3,):
0 Thank You
1 Its fine no problem
2 Are you sure
N.B. The numbers represent index in the required Pandas DF above.
Answers:
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:
#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 [1,2,3], you can do:
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
Example:
['Thank You',
'It's fine no problem',
'Are you sure?']
Code block:
import pandas as pd
df = pd.DataFrame(lst)
Output:
0
0 Thank You
1 It's fine no problem
2 Are you sure?
It is not recommended to remove the column names of the Pandas 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 Thank You
1 It's 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 Thank You
1 It's fine no problem
2 Are you sure?
You can directly call the pd.DataFrame()
method and pass your list as the parameter.
import pandas as pd
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
For converting a list into Pandas core data frame, we need to use DataFrame method from the pandas package.
There are different ways to perform the above operation (assuming Pandas is imported as pd
)
pandas.DataFrame({'Column_Name':Column_Data})
- Column_Name : String
- Column_Data : List form
-
Data = pandas.DataFrame(Column_Data)`
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})
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']
I need to convert my list into a one-column pandas dataframe.
Current List (len=3):
['Thanks You',
'Its fine no problem',
'Are you sure']
Required Pandas DF (shape =3,):
0 Thank You
1 Its fine no problem
2 Are you sure
N.B. The numbers represent index in the required Pandas DF above.
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:
#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 [1,2,3], you can do:
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
Example:
['Thank You',
'It's fine no problem',
'Are you sure?']
Code block:
import pandas as pd
df = pd.DataFrame(lst)
Output:
0
0 Thank You
1 It's fine no problem
2 Are you sure?
It is not recommended to remove the column names of the Pandas 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 Thank You
1 It's 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 Thank You
1 It's fine no problem
2 Are you sure?
You can directly call the pd.DataFrame()
method and pass your list as the parameter.
import pandas as pd
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
For converting a list into Pandas core data frame, we need to use DataFrame method from the pandas package.
There are different ways to perform the above operation (assuming Pandas is imported as pd
)
pandas.DataFrame({'Column_Name':Column_Data})
- Column_Name : String
- Column_Data : List form
-
Data = pandas.DataFrame(Column_Data)` 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})
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']