简体   繁体   中英

Python Pandas CSV Converting Int64 to the Object and call the right row via input

I am new in Python Pandas and I am trying to figure it out the problem.

I am fighting with the problem of converting dtype value in my csv.

I wrote a simple example to understand what is the problem but I cannot see there anything and I am not able to find why it is not working .. Please see below.

I have now a CSV table with 3 columns For the A and B the dtypes is Int64 for C it is object If i will set the variable as str it will change the value from int64 to object.

My code is like this :

import pandas as pd

data_Cisla = pd.read_csv("Cisla.csv", sep=";" , dtype=str)

print(data_Cisla.dtypes)
print(data_Cisla)


def cisla():
    vstup = input("Input value ")

    print(vstup, type(vstup))

    print(data_Cisla.loc[vstup])

When I will use also index_col="C" and print the cisla()

It is working. Program will ask me for an input from the Column C - So I write for example text_2 and it give me output (C)text_2 (A) 2 (B) 20 ----> This is what I am looking for but for the column A as an index_col.

But if I will use the same thing for index_col A an write 20 when program ask for Input value it doesn´t work and giving me error ..

What I don´t understand is When I am printing each step with data_Cisla.dtypes it will say me that all the time all column are object so what is the differences there ? Why it is working for column C and not for column A?

Final code looks like this

import pandas as pd

data_Cisla = pd.read_csv("Cisla.csv", sep=";" , dtype=str, index_col="C")

def cisla():
    vstup = input("Input value ")


    print(data_Cisla.loc[vstup])



cisla()

Thank you for helping me.

The reason for the observed behavior is that column 'C' is your index. I do not know why, because it is not in your code. My solution:

import pandas as pd

# build test data
data_Cisla = [[1, 10, 'text_1'],
             [2, 20, 'text_2'],]


data_Cisla = pd.DataFrame.from_records(data=data_Cisla, columns=['A', 'B', 'C'])

data_Cisla = data_Cisla.reset_index()

def cisla(data_Cisla: pd.DataFrame, col: str, vstup: str):  
    # Do not change data_Cisla, just make sure vstup is in the right format (str or float)
    try:
        vstup = float(vstup)
    except ValueError:
        pass

    mask = data_Cisla[col] == vstup    
    return data_Cisla[mask]

It will produce the following result:

cisla(data_Cisla, 'C', 'text_1')  #-> 1 | 10 | text_1
cisla(data_Cisla, 'A', '1')  #-> -> 1 | 10 | text_1
cisla(data_Cisla, 'A', 1)  #-> -> 1 | 10 | text_1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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