--I want to convert DataFrame, Series, and standard list used in pandas to each other. --When using data processed by pandas in other libraries, it may only support list, so don't worry.
-Convert pandas.DataFrame, Series and Python standard list to each other -Pandas User Guide "Indexing and Selecting Data" (Official Document Japanese Translation)
--Convert from list to dataframe, series --Convert from series to list --Convert from dataframe to list, series
list→series
#Import pandas and rename it to pd
import pandas as pd
#View from Create List of Standard Library
l_1d = [0, 1, 2]
l_1d
#[0, 1, 2]
#Next, type convert list to series
s = pd.Series(l_1d)
s
#0 0
#1 1
#2 2
#dtype: int64
#If nothing is specified, index is automatically determined from 0
#Next, convert from list to series type while specifying index
s = pd.Series(l_1d, index=['row1', 'row2', 'row3'])
s
#row1 0
#row2 1
#row3 2
#dtype: int64
#You can also specify up to index at once
#It has nothing to do with this article, but it may be used for other purposes
s = pd.Series({"row1":0,"row2":1,"row3":2})
s
#row1 0
#row2 1
#row3 2
#dtype: int64
list→dataframe
#Prepare a two-dimensional array for conversion to a data frame
l_2d = [[0, 1, 2], [3, 4, 5]]
df = pd.DataFrame(l_2d)
df
# 1 2
#0 0 1 2
#1 3 4 5
#index,It is also possible to create a dataframe by specifying both columns
df = pd.DataFrame(l_2d,
index=['row1', 'row2'],
columns=['col1', 'col2', 'col3'])
df
# col1 col2 col3
#row1 0 1 2
#row2 3 4 5
#Index at once,You can also specify up to column
#It has nothing to do with this article, but it may be used for other purposes
df = pd.DataFrame([[1,2,3],[4,5,6]],["row1","row2"],["col1","col2","col3"])
df
# col1 col2 col3
#row1 1 2 3
#row2 4 5 6
series→list
#First, create a series
s = pd.Series([0, 1, 2])
print(s)
# 0 0
# 1 1
# 2 2
# dtype: int64
#Get only the value of series as list type
l_1d = s.values.tolist()
print(l_1d)
#[0, 1, 2]
dataframe→list
#Create dataframe
df = pd.DataFrame([[0, 1, 2], [3, 4, 5]])
print(df)
# 0 1 2
#0 0 1 2
#1 3 4 5
#Get only the value of dataframe as list type
#index and columns are ignored
l_2d = df.values.tolist()
print(l_2d)
#[[0, 1, 2], [3, 4, 5]]
#If you get the dataframe as a list type, it becomes a two-dimensional array.
#How to convert 2D to 1D array
#It doesn't matter, but if there is, maybe I'll use it someday?
#I will use numpy so I will import it
import numpy as np
#Convert from standard library list type to array type
#Both are arrays
arr_list = np.array(l_2d)
arr_list.flatten().tolist()
#[0, 1, 2, 3, 4, 5]
dataframe→series
#Create a dataframe
df = pd.DataFrame([[0, 1, 2], [3, 4, 5]],["row1","row2"],["col1","col2","col3"])
print(df)
# col1 col2 col3
#row1 0 1 2
#row2 3 4 5
#Get column as series
df_toSer = df["col1"]
df_toSer
#row1 0
#row2 3
#Name: col1, dtype: int64
#Get index as series
df_toSer2 = df.iloc[0]
df_toSer2
#col1 0
#col2 1
#col3 2
#Name: row1, dtype: int64
This is the site that I referred to this time. -Convert pandas.DataFrame, Series and Python standard list to each other -Pandas User Guide "Indexing and Selecting Data" (Official Document Japanese Translation)
Recommended Posts