如何在 Pandas DataFrame 中将字符串转换为整数?
让我们看看在 Pandas DataFrame 中将字符串转换为整数的方法:
方法一:使用Series.astype()方法。
Syntax: Series.astype(dtype, copy=True, errors=’raise’)
Parameters: This method will take following parameters:
- dtype: Data type to convert the series into. (for example str, float, int).
- copy: Makes a copy of dataframe/series.
- errors: Error raising on conversion to invalid data type. For example dict to string. ‘raise’ will raise the error and ‘ignore’ will pass without raising error.
Return: Series with changed data type.
最有效的方法之一是 Pandas astype()。它用于修改一组数据类型。当从 csv 文件创建数据框时导入列,并且自动配置数据类型,这几次不是它应该具有的。例如,工资列可以作为字符串导入,但我们必须将其转换为浮点数才能进行操作。
示例 1:
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Name': ['GeeksForGeeks','Python'],
'Unique ID': ['900','450']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert string to an integer
df['Unique ID'] = df['Unique ID'].astype(int)
# show the dataframe
print (df)
print("-"*25)
# show the data types
# of each columns
print (df.dtypes)
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Algorithm': ['Graph', 'Dynamic Programming',
'Number Theory',
' Sorting And Searching'],
'Problems': ['62', '110', '40', '55']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert string to integer
df['Problems'] = df['Problems'].astype(int)
# show the dataframe
print (df)
print("-"*25)
# show the data type
# of each columns
print (df.dtypes)
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Name': ['GeeksForGeeks','Python'],
'Unique ID': ['900','450']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert integer to string
df['Unique ID'] = pd.to_numeric(df['Unique ID'])
# show the dataframe
print (df)
print("-"*30)
# show the data type
# of each columns
print (df.dtypes)
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Algorithm': ['Graph', 'Dynamic Programming',
'Number Theory',
' Sorting And Searching'],
'Problems': ['62', '110', '40', '55']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert string to an integer
df['Problems'] = pd.to_numeric(df['Problems'])
# show the dataframe
print (df)
print("-"*30)
# show the data type
# of each column
print (df.dtypes)
输出 :
示例 2:
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Algorithm': ['Graph', 'Dynamic Programming',
'Number Theory',
' Sorting And Searching'],
'Problems': ['62', '110', '40', '55']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert string to integer
df['Problems'] = df['Problems'].astype(int)
# show the dataframe
print (df)
print("-"*25)
# show the data type
# of each columns
print (df.dtypes)
输出 :
方法二:使用pandas 。 to_numeric () 方法。
Syntax: pandas.to_numeric(arg, errors=’raise’, downcast=None)
Parameters: This method will take following parameters:
- arg: list, tuple, 1-d array, or Series.
- errors: {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
-> If ‘raise’, then invalid parsing will raise an exception
-> If ‘coerce’, then invalid parsing will be set as NaN
-> If ‘ignore’, then invalid parsing will return the input - downcast: [default None] If not None, and if the data has been successfully cast to a numerical dtype downcast that resulting data to the smallest numerical dtype possible according to the following rules:
-> ‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8)
-> ‘unsigned’: smallest unsigned int dtype (min.: np.uint8)
-> ‘float’: smallest float dtype (min.: np.float32)
Returns: numeric if parsing succeeded. Note that return type depends on input. Series if Series, otherwise ndarray.
pandas.to numeric() 是在 Pandas 中将参数转换为数字形式的广泛使用的方法之一。
示例 1:
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Name': ['GeeksForGeeks','Python'],
'Unique ID': ['900','450']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert integer to string
df['Unique ID'] = pd.to_numeric(df['Unique ID'])
# show the dataframe
print (df)
print("-"*30)
# show the data type
# of each columns
print (df.dtypes)
输出 :
示例 2:
Python3
# import pandas library
import pandas as pd
# dictionary
Data = {'Algorithm': ['Graph', 'Dynamic Programming',
'Number Theory',
' Sorting And Searching'],
'Problems': ['62', '110', '40', '55']}
# create a dataframe object
df = pd.DataFrame(Data)
# convert string to an integer
df['Problems'] = pd.to_numeric(df['Problems'])
# show the dataframe
print (df)
print("-"*30)
# show the data type
# of each column
print (df.dtypes)
输出 :