如何将 Pandas DataFrame 列转换为系列?
在熊猫中可以将熊猫数据框的列转换为系列。有时需要将数据框的列转换为其他类型(如系列)以分析数据集。
案例1:将数据框的第一列转换为Series
Python3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August': [10, 25, 34, 4.85, 71.2, 1.1],
'September': [4.8, 54, 68, 9.25, 58, 0.9],
'October': [78, 5.8, 8.52, 12, 1.6, 11],
'November': [100, 5.8, 50, 8.9, 77, 10] }
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
Python3
# Converting first column i.e 'August' to Series
ser1 = df.ix[:,0]
print("\n1st column as a Series:\n")
print(ser1)
# Checking type
print(type(ser1))
Python3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August': [10, 25, 34, 4.85, 71.2, 1.1],
'September': [4.8, 54, 68, 9.25, 58, 0.9],
'October': [78, 5.8, 8.52, 12, 1.6, 11],
'November': [100, 5.8, 50, 8.9, 77, 10] }
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
Python3
# Converting last column i.e 'November' to Series
ser1 = df.ix[:,3]
print("\nLast column as a Series:\n")
print(ser1)
# Checking type
print(type(ser1))
Python3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August': [10, 25, 34, 4.85, 71.2, 1.1],
'September': [4.8, 54, 68, 9.25, 58, 0.9],
'October': [78, 5.8, 8.52, 12, 1.6, 11],
'November': [100, 5.8, 50, 8.9, 77, 10] }
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
Python3
# Converting multiple columns
# i.e 'September' and 'October' to Series
ser1 = df.ix[:,1]
ser2 = df.ix[:,2]
print("\nMultiple columns as a Series:\n")
print(ser1)
print()
print(ser2)
# Checking type
print(type(ser1))
print(type(ser2))
输出:
将第一列转换为系列。
蟒蛇3
# Converting first column i.e 'August' to Series
ser1 = df.ix[:,0]
print("\n1st column as a Series:\n")
print(ser1)
# Checking type
print(type(ser1))
输出:
在上面的示例中,我们将列“ August ”的类型从数据框更改为Series 。
案例 2:将数据框的最后一列转换为Series
蟒蛇3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August': [10, 25, 34, 4.85, 71.2, 1.1],
'September': [4.8, 54, 68, 9.25, 58, 0.9],
'October': [78, 5.8, 8.52, 12, 1.6, 11],
'November': [100, 5.8, 50, 8.9, 77, 10] }
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
输出:
将最后一列转换为系列。
蟒蛇3
# Converting last column i.e 'November' to Series
ser1 = df.ix[:,3]
print("\nLast column as a Series:\n")
print(ser1)
# Checking type
print(type(ser1))
输出:
在上面的示例中,我们将列“十一月”的类型从数据框更改为系列。
案例3:将数据框的多列转换为Series
蟒蛇3
# Importing pandas module
import pandas as pd
# Creating a dictionary
dit = {'August': [10, 25, 34, 4.85, 71.2, 1.1],
'September': [4.8, 54, 68, 9.25, 58, 0.9],
'October': [78, 5.8, 8.52, 12, 1.6, 11],
'November': [100, 5.8, 50, 8.9, 77, 10] }
# Converting it to data frame
df = pd.DataFrame(data=dit)
# Original DataFrame
df
输出:
将多列转换为系列。
蟒蛇3
# Converting multiple columns
# i.e 'September' and 'October' to Series
ser1 = df.ix[:,1]
ser2 = df.ix[:,2]
print("\nMultiple columns as a Series:\n")
print(ser1)
print()
print(ser2)
# Checking type
print(type(ser1))
print(type(ser2))
输出:
在上面的示例中,我们将 2 列的类型(即“九月”和“十月”)从数据框更改为Series 。