📜  使用 for 循环创建 pandas 列

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

使用 for 循环创建 pandas 列

让我们看看如何使用 for 循环在 pandas 数据框中创建列。当我们需要处理之前为此目的创建的数据框的数据时,有时需要这种操作,我们需要这种类型的计算,以便我们可以处理现有数据并创建一个单独的列来存储数据。

它可以通过for循环轻松完成。列的数据可以从现有的 Dataframe 或任何数组中获取。

# importing libraries
import pandas as pd
import numpy as np
  
raw_Data = {'Voter_name': ['Geek1', 'Geek2', 'Geek3', 'Geek4', 
                           'Geek5', 'Geek6', 'Geek7', 'Geek8'], 
            'Voter_age': [15, 23, 25, 9, 67, 54, 42, np.NaN]}
  
df = pd.DataFrame(raw_Data, columns = ['Voter_name', 'Voter_age'])
#       //DataFrame will look like
#
# Voter_name          Voter_age
# Geek1                15
# Geek2                23
# Geek3                25
# Geek4                09
# Geek5                67
# Geek6                54
# Geek7                42
# Geek8           not a number
  
eligible = []
  
# For each row in the column
for age in df['Voter_age']:       
    if age >= 18:                   # if Voter eligible
        eligible.append('Yes')
    elif age < 18:                  # if voter is not eligible
        eligible.append("No")
    else:
        eligible.append("Not Sure")
  
# Create a column from the list
df['Voter'] = eligible  
            
print(df)
输出:
Voter_name  Voter_age     Voter
0      Geek1         15        No
1      Geek2         23       Yes
2      Geek3         25       Yes
3      Geek4          9        No
4      Geek5         67       Yes
5      Geek6         54       Yes
6      Geek7         42       Yes
7      Geek8        NaN  Not Sure