“Pandas Zmień typ kolumny” Kod odpowiedzi

Python: transformuj jako typ numeirc

df['myvar'] = df['myvar'].astype(str)   # Transform as character
df['myvar'] = df['myvar'].astype(float) # Transform as float
df['myvar'] = df['myvar'].astype(int)   # Transform as numeric
Andrea Perlato

Zmień typ kolumny DataFrame

>>> df.astype({'col1': 'int32'}).dtypes
col1    int32
col2    int64
dtype: object
Obedient Oryx

Konwertuj kolumnę pandas na int

# convert Series
my_series = pd.to_numeric(my_series)

# convert column "a" of a DataFrame
df["a"] = pd.to_numeric(df["a"])
Courageous Cobra

Pandy zmieniają się na numeryczne

>>> s = pd.Series(["8", 6, "7.5", 3, "0.9"]) # mixed string and numeric values
>>> s
0      8
1      6
2    7.5
3      3
4    0.9
dtype: object

>>> pd.to_numeric(s) # convert everything to float values
0    8.0
1    6.0
2    7.5
3    3.0
4    0.9
dtype: float64
Worrisome Wallaby

Pandas Zmień typ kolumny

# select columns that need to be converted
cols = df.select_dtypes(include=['float64']).columns.to_list()
df = df.astype({col:int for col in cols})
Combative Crocodile

Ustaw typ pand kolumnowych

df.astype(int)
Real Rook

Odpowiedzi podobne do “Pandas Zmień typ kolumny”

Pytania podobne do “Pandas Zmień typ kolumny”

Więcej pokrewnych odpowiedzi na “Pandas Zmień typ kolumny” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu