Pandas DataFrame中整數最快轉換為字串的方法


在Python中,有很多方法可以將Pandas DataFrame中的整數轉換為字串,例如astype()方法、apply()方法、map()方法、列表推導式等等。在所有這些方法中,可以透過跟蹤每種方法的轉換時間來確定將Pandas DataFrame中的整數轉換為字串的最快方法。在本文中,我們將瞭解如何使用所有四種方法將Pandas DataFrame中的整數轉換為字串,然後跟蹤每種方法的轉換時間。

方法一:使用列表推導式方法

在這種方法中,我們透過迭代整數列並將每個整數轉換為字串值來建立字串值列表,使用列表推導式。

示例

在下面的示例中,我們建立了一個包含整數值的示例Pandas DataFrame。為了將這些值轉換為字串值,我們使用列表推導式建立一個字串值列表,並將字串值列表賦給整數列。

import pandas as pd

# create a sample data frame with an integer column
df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]})

# create a list of string values using a list comprehension
str_list = [str(i) for i in df['int_column']]

# assign the list of string values to the integer column
df['int_column'] = str_list

# print the data frame
print(df)

輸出

int_column
0          1
1          2
2          3
3          4
4          5

方法二:使用astype()方法

astype()方法將整列從一種資料型別轉換為另一種資料型別。然後將該列的每個元素從一種資料型別強制轉換為另一種資料型別。

示例

在下面的示例中,我們建立了一個包含整數列的示例DataFrame,然後使用astype()函式將該列轉換為字串。

import pandas as pd

# create a sample data frame with an integer column
df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]})

# convert the integer column to a string column
df['int_column'] = df['int_column'].astype(str)

# print the data frame
print(df)

輸出

  int_column
0          1
1          2
2          3
3          4
4          5

方法三:使用apply()方法

apply方法對列的每個元素應用一個函式。使用lambda函式,我們可以將其應用於列的每個元素,並將其從整數轉換為字串。

示例

在下面的示例中,我們建立了一個包含整數列的示例DataFrame,然後定義一個lambda函式來將整數轉換為字串,並將該lambda函式應用於該列的每個元素。

import pandas as pd

# create a sample data frame with an integer column
df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]})

# define a lambda function to convert integers to strings
int_to_str = lambda x: str(x)

# apply the lambda function to the integer column
df['int_column'] = df['int_column'].apply(int_to_str)

# print the data frame
print(df)

輸出

  int_column
0          1
1          2
2          3
3          4
4          5

方法四:使用map()方法

map()方法也可以對映到列的每個元素。可以建立一個lambda函式來將整數轉換為字串值,並將其對映到整數列。

示例

import pandas as pd

# create a sample data frame with an integer column
df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]})

# define a lambda function to convert integers to strings
int_to_str = lambda x: str(x)

# map the lambda function to the integer column
df['int_column'] = df['int_column'].map(int_to_str)

# print the data frame
print(df)

輸出

  int_column
0          1
1          2
2          3
3          4
4          5

比較所有四種方法

我們可以編寫一段程式碼來測量每種方法在Pandas DataFrame中將整數轉換為字串所花費的時間。花費時間最少的方法就是最快的方法。

示例

import pandas as pd
import time

import pandas as pd
import time

# create a sample data frame with an integer column
df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]*10000})

# Method 1: Using the astype() method
start_time = time.time()
df['int_column'] = df['int_column'].astype(str)
method1_time = time.time() - start_time
print("Time taken for Method 1: ", method1_time)

# Method 2: Using the apply() method
start_time = time.time()
int_to_str = lambda x: str(x)
df['int_column'] = df['int_column'].apply(int_to_str)
method2_time = time.time() - start_time
print("Time taken for Method 2: ", method2_time)

# Method 3: Using the map() method
start_time = time.time()
int_to_str = lambda x: str(x)
df['int_column'] = df['int_column'].map(int_to_str)
method3_time = time.time() - start_time
print("Time taken for Method 3: ", method3_time)

# Method 4: Using the list comprehension
start_time = time.time()
str_list = [str(i) for i in df['int_column']]
df['int_column'] = str_list
method4_time = time.time() - start_time
print("Time taken for Method 4: ", method4_time)

# Determine the fastest method
times = {'Method 1': method1_time,
      'Method 2': method2_time,
      'Method 3': method3_time,
      'Method 4': method4_time}
fastest_method = min(times, key=times.get)
print("The fastest method is:", fastest_method)

輸出

Time taken for Method 1:  0.03693246841430664
Time taken for Method 2:  0.023466110229492188
Time taken for Method 3:  0.02350783348083496
Time taken for Method 4:  0.027480602264404297
The fastest method is: Method 3

上面的輸出顯示,最快的方法是apply()方法。

結論

在Pandas DataFrame中將整數轉換為字串的最快方法是apply()方法。在本文中,我們瞭解了在Pandas DataFrame中將整數轉換為字串的所有方法,並比較了所有方法以找出最快的方法。

更新於:2023年7月10日

3000+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告