簡體   English   中英

Python函數更改列的數據類型不起作用

[英]Python function to change data type of a column not working

我編寫了一個python函數,以接收數據框的一列,檢查數據類型,以及是否對所需的數據類型進行了錯誤更改。 但是,更改僅在函數內發生。 如何解決此問題以對數據框進行永久更改?

def change_required_data_type (column,data_type):
    is_correct = None

    for i in column:
        if type(i) != data_type:
            is_correct = False


    if is_correct != False:
        print('True')

    elif is_correct == False:
        column = column.astype(data_type)        
        print('False')

對於僅在函數內部起作用而不在外部起作用的問題,您需要在函數的末尾添加return一些對象

def myfunc(column, data_type):
    # ...
    elif is_correct == False:
    column = column.astype(data_type)        
    print('False')

    # You've modified the column variable inside the function,
    # so your function needs to return it to outside the function.        
    return column

# Call your function from outside.
result = myfunc(column, data_type)  
# Use inputs for column and data_type when calling your function.
print(result)

但是,如果使用的是Pandas庫,則應使用常規方法來更改列的數據類型。 參見https://cmdlinetips.com/2018/09/how-to-change-data-type-for-one-or-more-columns-in-pandas-dataframe/

通常,您想使用df.astype(str)來更改Pandas數據df.astype(str)一列或多列的數據類型。 數據框的單列也稱為系列。

df['Column1'] = df['Column1'].astype('str')
df.Day = df.Day.astype('int64')

這是在Pandas DataFrame對象中更改數據類型的更多示例。

import pandas as pd

mydic = {
    "name": ['Alice', 'Tommy', 'Jane'],
    "age": [9, 21, 22],
    "height": [3.6, 6.1, 5.5],
}

df = pd.DataFrame(data = mydic)
print(df)
print(df.dtypes)

# First change age from integer to floating point.
df.age = df.age.astype('float64')

print(df.age)  # Notice the decimal format 9.0.
print(df.dtypes)  # age is now floating point.

# Next change height from floating point to integer.
df.height = df.height.astype('int32')
print(df.height)  # Notice height is truncated, no more decimal point.

# Next change age to string (object type).
df.age = df.age.astype('str')
print(df.dtypes)
print(df)

# Change height from integer to float, using Bracket Notation.
df['height'] = df['height'].astype('float32')
print(df.dtypes)
# Notice height is a decimal format, 3.0.
# But the original fractional data of .6 was lost (was 3.6).

df.astype('str')的默認用法是返回COPY,而不替換原始數據幀。 因為您已使用df.name = ...將更改分配給原始系列,所以您更改了“就地”類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM