比较列的 Dask 数据框条件不起作用

问题描述 投票:0回答:1

我试图比较 dask 数据框的列名称,然后更改列数据类型,但是我的条件从来都不是真的:

column_name = "names"
print(f"Col Name: {column_name} \n") # names
# Change column datatype:
for i in dataframe_for_db.select_dtypes(include='object').columns.tolist():
    if (dataframe_for_db[i] == column_name).any().compute() :
        # Change column datatype
        print("Column found. changing datatype : ")
        dataframe_for_db[i] = dataframe_for_db[i].astype(str)

print(dataframe_for_db.dtypes)

正如你们所看到的,我得到了所有的列名称,但这不起作用。列名已在上面设置。

这个条件永远不会被执行:

if (dataframe_for_db[i] == column_name).any().compute() :

我想更改列数据类型,因为我需要将此数据框存储在我的数据库中。我只会在数据库中创建表名称,然后运行我的脚本,这将创建我的表的列。

任何解决这个问题的想法,我将不胜感激,非常感谢。

python dataframe dask
1个回答
0
投票
 import dask
 
 dataframe_for_db = dask.datasets.timeseries()¬
 print(dataframe_for_db.dtypes)
 column_name = "x"
 print(f"Col Name: {column_name} \n") # names
 # Change column datatype:
 for i in dataframe_for_db.columns:
     print(f"Processing column {i}")
     if (i == column_name):
         # Change column datatype
         print("Column found. changing datatype : ")
         dataframe_for_db[i] = dataframe_for_db[i].astype(str)

print("After Processing")
print(dataframe_for_db.dtypes)

产量

name     object
id        int64
x       float64
y       float64
dtype: object
Col Name: x

Processing column name
Processing column id
Processing column x
Column found. changing datatype :
Processing column y

After Processing
name     object
id        int64
x        object
y       float64
dtype: object
© www.soinside.com 2019 - 2024. All rights reserved.