1. 程式人生 > 實用技巧 >Pandas系列教程(4)Pandas新增資料列

Pandas系列教程(4)Pandas新增資料列

Pandas新增資料列

在進行資料分析時,經常需要按照一定的條件建立新的資料列,然後進行進一步分析

  1. 直接複製

  2. df.apply方法

  3. df.assign方法

  4. 按照條件選擇分組分別賦值

1、讀取csv資料到dataframe

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
print(df.head())

2、直接賦值方法

例項:清理溫度列,變成數字列

# 設定索引為日期,方便按日期篩選
df.set_index('ymd', inplace=True)
# 替換溫度的字尾℃ df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32') df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

例項:計算溫度差

# 注意df['bWendu']其實是一個Series,後面的減法返回的是Series
df.loc[:, 'wencha'] = df['bWendu'] - df['yWendu']

完整程式碼:

import pandas as pd

file_path 
= "../files/beijing_tianqi_2018.csv" df = pd.read_csv(file_path) # 替換溫度的字尾℃, 並轉為int32(修改列) df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32') df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32') print(df.head()) print('*' * 50, '\n') # 計算溫度差(新增列) #
注意df['bWendu']其實是一個Series,後面的減法返回的是Series df.loc[:, 'wencha'] = df['bWendu'] - df['yWendu'] print(df.head())

3、df.apply方法

例項:新增一列溫度型別

  1. 如果溫度大於33度就是高溫

  2. 低於-10度就是低溫

  3. 否則是常溫

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)

# 替換溫度的字尾℃, 並轉為int32(修改列)
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

print(df.head())
print('*' * 50, '\n')


def get_wendu_type(x):
    if x['bWendu'] > 33:
        return "高溫"
    elif x['yWendu'] < -10:
        return "低溫"
    else:
        return "常溫"


# 注意需要設定axis--1,這時Series的index是columns
df.loc[:, 'wendu_type'] = df.apply(get_wendu_type, axis=1)
# 列印前幾行資料
print(df.head())
print('*' * 50, '\n')
# 檢視溫度型別的計數
print(df['wendu_type'].value_counts())

4、df.assign方法

例項:將溫度從攝氏度變成華氏度

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)

# 替換溫度的字尾℃, 並轉為int32(修改列)
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

print(df.head())
print('*' * 50, '\n')

df_huashi = df.assign(
    yWendu_huashi=lambda x: x['yWendu'] * 9 / 5 + 32,
    bWendu_huashi=lambda x: x['bWendu'] * 9 / 5 + 32
)

print(df_huashi.head())
print('*' * 50, '\n')

5、按條件選擇分組分別賦值

按條件先選擇資料,然後對著部分資料賦值新列

例項:高低溫差大於10度,則認為溫差較大

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)

# 替換溫度的字尾℃, 並轉為int32(修改列)
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 列印前幾行資料
print(df.head())
print('*' * 50, '\n')

# 先建立空列(這是第一種建立新列的方法)
df['wencha_type'] = ""

df.loc[df['bWendu'] - df['yWendu'] > 10, 'wencha_type'] = "溫差大"
df.loc[df['bWendu'] - df['yWendu'] <= 10, 'wencha_type'] = "溫差正常"

# 列印前幾行資料
print(df.head())
print('*' * 50, '\n')

# 檢視溫差型別的計數
print(df['wencha_type'].value_counts())