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
。Pandas 可以使用方法更改列值的数据类型.astype()
。但这不是字符串的有效方法。有没有办法将输入字符串的数据类型转换为任意给定对象的相同数据类型?
解决方案
推荐阅读
- xml - 验证 XML:同名节点按属性值区分
- node.js - AdonisJS V4 - belongsToMany - sync()/attach() - uuid 类型的输入语法无效:“”
- java - 401 - 服务器上需要身份验证错误
- vb.net - Discord.Net 在哪里放置 SetGameAsync()
- c# - 如何在 C# 控制台中读取 excel 文件?
- php - 通过 GET 方法发送 http 请求
- python - 选项卡关闭时的 Django 过期会话
- tableau-api - Tableau 将维度转换为度量
- c# - 如何从连续的数据c#中形成5分钟的开高低收盘价?
- elasticsearch - Kibana v 7.9.2 hour_minute_second 的确切格式