📌  相关文章
📜  如何在 Pandas DataFrame 中将字符串转换为整数?

📅  最后修改于: 2022-05-13 01:55:46.700000             🧑  作者: Mango

如何在 Pandas DataFrame 中将字符串转换为整数?

让我们看看在 Pandas DataFrame 中将字符串转换为整数的方法:

方法一:使用Series.astype()方法。

最有效的方法之一是 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)

输出 :

具有数据类型的数据框

方法二:使用pandasto_numeric () 方法。

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)

输出 :

具有数据类型的数据框