如何将字符串的数据类型更改为或有值的相同数据类型?

2024-06-08 05:12:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我´我正在尝试实现一个过滤代码:

例如:

import pandas as pd   
import operator              #this lets you do logical operations

df = pd.read_csv('file.csv')

Filter_column = input('> ')  # I will do a filter in this column
comparision = input('> ')    # Here goes the logical comparission (==, >, <, etc)
filter_value = input('> ')   # Here goes the value to do the filter 

if comparision == '=':       #Say comparision = '=' in this example

    filter_value.astype(type(df['Filter_column)].dropna()[0])) #!!!
    filter = operator.eq(df['Filter_column'],filter_value))
    dp = df[filter]

然后,dp是给定过滤条件的过滤数据集

在我放置注释的行中,#!!!是转换输入字符串filter_value的数据类型的理想方法。熊猫可以使用.astype()方法更改列值的数据类型。 但这不是字符串的有效方法。有没有办法将输入字符串的数据类型转换为任意给定对象的相同数据类型


Tags: the方法字符串importdfinputvaluecolumn

热门问题