简体   繁体   English

如何将 dataframe 的列名从字符串转换为 python 中的元组?

[英]How to convert column name of dataframe from string to tuple in python?

I have a dataframe column names which are in form of string I would like to convert it into tuple, which shows the data inside of those column names.我有一个 dataframe 列名,它是字符串形式的,我想将其转换为元组,它显示了这些列名中的数据。 I have used "eval" but since its dangerous to use is there any other way to convert?我使用过“eval”,但由于使用起来很危险,还有其他方法可以转换吗?

input: dt = "df['cars'],df['bikes']"
output: dt = (df['cars'],df['bikes'])

use spit(',') method使用 spit(',') 方法

dt = "df['cars'],df['bikes']"
tu = tuple(dt.split(','))
print(tu)

output: ("df['cars']", "df['bikes']") output: ("df['cars']", "df['bikes']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM