pandas DataFrame 根據其他列新建列並賦值

主要是DataFrame.apply函數的應用

如果設置axis參數爲0則每次函數會取出DataFrame的一行來做處理;

如果設置axis參數爲1則每次函數會取出DataFrame的一列來做處理。

如代碼所示,判斷數學和英語均大於75分的同學,則新列test值賦爲1,否則爲0。

import pandas as pd

data = {
    'name': ["one", "two", "three", "four", "five", "six", "seven"],
    'math': [99, 65, 78, 43, 88, 75, 36],
    'English': [85, 74, 92, 76, 86, 36, 72]
}
frame = pd.DataFrame(data, columns=['name', 'math', 'English'])


# 查找數學和英語均大於75分的同學
def function(a, b):
    if a > 75 and b > 75:
        return 1
    else:
        return 0


print(frame)
# 兩種格式都可以
# frame['test'] = frame.apply(lambda x: function(x.math, x.English), axis=1)
frame['test'] = frame.apply(lambda x: function(x["math"], x["English"]), axis=1)
print(frame)

運行結果如下:

    name  math  English
0    one    99       85
1    two    65       74
2  three    78       92
3   four    43       76
4   five    88       86
5    six    75       36
6  seven    36       72
    name  math  English  test
0    one    99       85     1
1    two    65       74     0
2  three    78       92     1
3   four    43       76     0
4   five    88       86     1
5    six    75       36     0
6  seven    36       72     0

另外Series類型也有apply函數,用法示例如下:

import pandas as pd

data = {
    'name': ["one", "two", "three", "four", "five", "six", "seven"],
    'math': [99, 65, 78, 43, 88, 75, 36],
    'English': [85, 74, 92, 76, 86, 36, 72]
}
frame = pd.DataFrame(data, columns=['name', 'math', 'English'])

print(frame)
# 判斷數學成績是否及格
frame['test'] = frame.math.apply(lambda x: 1 if x >= 60 else 0)
print(frame)

運行效果如下:

    name  math  English
0    one    99       85
1    two    65       74
2  three    78       92
3   four    43       76
4   five    88       86
5    six    75       36
6  seven    36       72 

    name  math  English  test
0    one    99       85     1
1    two    65       74     1
2  three    78       92     1
3   four    43       76     0
4   five    88       86     1
5    six    75       36     1
6  seven    36       72     0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章