問題描述
我有一個 DataFrame
.以下是兩個相關的列:一個是 int
的列,另一個是 str
的列.
I have a DataFrame
. Two relevant columns are the following: one is a column of int
and another is a column of str
.
我知道如果我將 NaN
插入 int
列,Pandas 會將所有 int
轉換為 float
因為 int
沒有 NaN
值.
I understand that if I insert NaN
into the int
column, Pandas will convert all the int
into float
because there is no NaN
value for an int
.
但是,當我將 None
插入 str
列時,Pandas 會將我的所有 int
轉換為 float
為好.這對我來說沒有意義 - 為什么我在第 2 列中輸入的值會影響第 1 列?
However, when I insert None
into the str
column, Pandas converts all my int
to float
as well. This doesn't make sense to me - why does the value I put in column 2 affect column 1?
這是一個簡單的工作示例(Python 2):
Here's a simple working example (Python 2):
import pandas as pd
df = pd.DataFrame()
df["int"] = pd.Series([], dtype=int)
df["str"] = pd.Series([], dtype=str)
df.loc[0] = [0, "zero"]
print df
print
df.loc[1] = [1, None]
print df
輸出是
int str
0 0 zero
int str
0 0.0 zero
1 1.0 NaN
有沒有辦法讓輸出如下:
Is there any way to make the output the following:
int str
0 0 zero
int str
0 0 zero
1 1 NaN
不將第一列重鑄為 int
.
我更喜歡使用
int
而不是float
因為實際數據在該列是整數.如果沒有解決方法,我只會使用float
.
I prefer using
int
instead offloat
because the actual data in that column are integers. If there's not workaround, I'll just usefloat
though.
我不喜歡重鑄,因為在我的實際代碼中,我不需要
存儲實際的dtype
.
I prefer not having to recast because in my actual code, I don't
store the actual dtype
.
我還需要逐行插入數據.
I also need the data inserted row-by-row.
推薦答案
如果你設置dtype=object
,你的系列就可以包含任意數據類型:
If you set dtype=object
, your series will be able to contain arbitrary data types:
df["int"] = pd.Series([], dtype=object)
df["str"] = pd.Series([], dtype=str)
df.loc[0] = [0, "zero"]
print(df)
print()
df.loc[1] = [1, None]
print(df)
int str
0 0 zero
1 NaN NaN
int str
0 0 zero
1 1 None
這篇關于阻止 Pandas 將 int 轉換為 float的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!