📌  相关文章
📜  如何在Python中将列表作为一行附加到 Pandas DataFrame 中?

📅  最后修改于: 2022-05-13 01:54:50.867000             🧑  作者: Mango

如何在Python中将列表作为一行附加到 Pandas DataFrame 中?

先决条件: Pandas DataFrame

在本文中,我们将看到如何在Python中将列表作为行附加到 Pandas 数据帧。它可以通过三种方式完成:

  • 使用 loc[]
  • 使用 iloc[]
  • 使用 append()

使用 loc[] 方法附加列表

Pandas DataFrame.loc属性通过标签或给定 DataFrame 中的布尔数组访问一组行和列。

让我们逐步添加列表:

步骤 1:使用列表创建一个简单的数据框。

Python3
import pandas as pd
 
# List
Person = [ ['Satyam', 21, 'Patna' , 'India' ],
            ['Anurag', 23, 'Delhi' , 'India' ],
            ['Shubham', 27, 'Coimbatore' , 'India' ]]
 
#Create a DataFrame object
df = pd.DataFrame(Person,
                  columns = ['Name' , 'Age', 'City' , 'Country'])
 
# display
display(df)


Python3
# New list for append into df
list = ["Saurabh", 23, "Delhi", "india"]
 
# using loc methods
df.loc[len(df)] = list
 
# display
display(df)


Python3
# import module
import pandas as pd
 
# List
Person = [ ['Satyam', 21, 'Patna' , 'India' ],
            ['Anurag', 23, 'Delhi' , 'India' ],
            ['Shubham', 27, 'Coimbatore' , 'India' ],
            ["Saurabh", 23, "Delhi", "india"]]
 
#Create a DataFrame object
df = pd.DataFrame(Person,
                  columns = ['Name' , 'Age', 'City' , 'Country'])
 
# new list to append into df
list = ['Ujjawal', 22, 'Fathua', 'India']
 
# using iloc
df.iloc[2] = list
 
# display
display(df)


Python3
# import module
import pandas as pd
 
# List
Person = [ ['Satyam', 21, 'Patna' , 'India' ],
            ['Anurag', 23, 'Delhi' , 'India' ],
            ['Shubham', 27, 'Coimbatore' , 'India' ]]
 
#Create a DataFrame object
df = pd.DataFrame(Person,
                  columns = ['Name' , 'Age', 'City' , 'Country'])
 
# new list to append into df
list = [["Manjeet", 25, "Delhi", "india"]]
 
# using append
df = df.append(pd.DataFrame( list,
               columns=[ 'Name', 'Age', 'City', 'Country']),
               ignore_index = True)
 
# display df
display(df)



输出:

第 2 步:使用 loc 将新列表附加到数据框中。

蟒蛇3

# New list for append into df
list = ["Saurabh", 23, "Delhi", "india"]
 
# using loc methods
df.loc[len(df)] = list
 
# display
display(df)


输出:

使用 iloc[] 方法附加列表

Pandas DataFrame.iloc方法访问基于整数位置的索引以按位置选择。

例子:

蟒蛇3

# import module
import pandas as pd
 
# List
Person = [ ['Satyam', 21, 'Patna' , 'India' ],
            ['Anurag', 23, 'Delhi' , 'India' ],
            ['Shubham', 27, 'Coimbatore' , 'India' ],
            ["Saurabh", 23, "Delhi", "india"]]
 
#Create a DataFrame object
df = pd.DataFrame(Person,
                  columns = ['Name' , 'Age', 'City' , 'Country'])
 
# new list to append into df
list = ['Ujjawal', 22, 'Fathua', 'India']
 
# using iloc
df.iloc[2] = list
 
# display
display(df)

输出:

注 – 它用于基于位置的索引,因此它仅适用于现有索引并替换行元素。

使用 append() 方法附加列表

Pandas dataframe.append()函数用于将其他数据帧的行附加到给定数据帧的末尾,返回一个新的数据帧对象。

例子:

蟒蛇3

# import module
import pandas as pd
 
# List
Person = [ ['Satyam', 21, 'Patna' , 'India' ],
            ['Anurag', 23, 'Delhi' , 'India' ],
            ['Shubham', 27, 'Coimbatore' , 'India' ]]
 
#Create a DataFrame object
df = pd.DataFrame(Person,
                  columns = ['Name' , 'Age', 'City' , 'Country'])
 
# new list to append into df
list = [["Manjeet", 25, "Delhi", "india"]]
 
# using append
df = df.append(pd.DataFrame( list,
               columns=[ 'Name', 'Age', 'City', 'Country']),
               ignore_index = True)
 
# display df
display(df)

输出: