Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取CSV并存储在Pandas中,并将特定列转换为int_Python_Pandas - Fatal编程技术网

Python 读取CSV并存储在Pandas中,并将特定列转换为int

Python 读取CSV并存储在Pandas中,并将特定列转换为int,python,pandas,Python,Pandas,我在csv中有2列,读取csv并将其存储在df中。一旦数据存储在df中,它就成为对象。我想将“A”列转换为int。例如: A B 1 2 1 3 3 4 4.5 df.dtypes将数据类型打印为对象。现在我想把这个对象转换成int64。 我做不到 尝试过的事情: df['A'] = pd.to_numeric(df['A'], errors="coerce") #converted to float64 df['A'] = df['A'].fillna('

我在csv中有2列,读取csv并将其存储在df中。一旦数据存储在df中,它就成为对象。我想将“A”列转换为int。例如:

A B 1 2 1 3 3 4 4.5

df.dtypes将数据类型打印为对象。现在我想把这个对象转换成int64。 我做不到

尝试过的事情:

df['A']    = pd.to_numeric(df['A'], errors="coerce") #converted to float64
df['A']    = df['A'].fillna('')
df['A']    = df['A'].astype('int64')
df['A']    = df['A'].astype('str').astype('int64')

它们都没有转换为int64。因为我需要这个列作为int,所以我需要使用它来比较其他列。感谢您的帮助。

尽管这很难看,但它仍然有效:

np.floor(pd.to_numeric(df['A'], errors='coerce')).astype(pd.Int64Dtype())

您也可以尝试这样做

df['A'] = pd.to_numeric(df['A'], errors="coerce").fillna(0).astype(int).to_frame()
上述代码不舍入为整数。如果你想把数字四舍五入,你可以给这个。
fillna(0)
.astype(int)
之后的
.round()
将向上取整。如果您希望将值向上舍入为整数,则这是一个选项

df['A'] = pd.to_numeric(df['A'], errors="coerce").fillna(0).round().astype(int).to_frame()
它将
NaN
值转换为
0
,然后将所有值转换为整数值。这样,您将获得所需的值

import pandas as pd
df = pd.DataFrame({'A':[1.8, 3.3, 5.2, 'Good', 7, 9,2],
                   'B':['Apple','Banana','Cherry','Donuts','Egg','Fig','Grape']})
print (df)
df['A'] = pd.to_numeric(df['A'], errors="coerce").fillna(0).astype(int).to_frame()
print (df)
这将把
df['A']
中的值转换为数值,同时将所有字符串设置为
NaN
,然后将这些
NaN
s转换为
0
,然后将所有值转换为int。由于这是一个系列,您需要将其转换回带有
to_frame()

上述代码的输出为:

原始数据帧:

      A       B
0   1.8   Apple
1   3.3  Banana
2   5.2  Cherry
3  Good  Donuts
4     7     Egg
5     9     Fig
6     2   Grape
   A       B
0  1   Apple
1  3  Banana
2  5  Cherry
3  0  Donuts
4  7     Egg
5  9     Fig
6  2   Grape
转换的数据帧:

      A       B
0   1.8   Apple
1   3.3  Banana
2   5.2  Cherry
3  Good  Donuts
4     7     Egg
5     9     Fig
6     2   Grape
   A       B
0  1   Apple
1  3  Banana
2  5  Cherry
3  0  Donuts
4  7     Egg
5  9     Fig
6  2   Grape

执行此操作后,请显示以下结果:df['a'].unique()['001''-'004''003''002'':001''401(''004~'005''001\n''009''(001'','014'])-df['a']=df['a'].str.replace(r'\D','')['001'''004''003''002''401 005''009 014']我的是一个包含500行的大数据帧。这个系列如何工作?我应该给出所有的值吗?这个“sa”是什么意思?这是一个示例系列:pd.series([1.2,1,'sa'])。对于df.df\u bot['extracted\u value']=pd.to\u numeric(df\u bot['extracted\u value'],errors=“concurve”).fillna('.astype(int).to\u frame())来说,500行非常小我尝试了上面的代码,但抛出了一个错误:ValueError:invalid literal for int(),以10为基数:“”您必须执行fillna(0.0)而不是fillna(“”)。否则它正试图将“”转换为intYes。我尝试了,它成功了。我正在使用此int64列和另一个int64列,并传递给sequenceMatcher函数,以比较两列并返回比率。我得到以下错误:TypeError:“int”对象不是iterable def similiariality\u ratio(df_merge,col1,col2):返回SequenceMatcher(无,df_merge[col1],df_merge[col2])。比率()我尝试过的事情:将对象从int64转换回object,但仍然是相同的错误。df_merge['PC_Value']=df_merge['PC_Value']。astype('object'))你能告诉我你的工作范围和期望的结果吗?我无法理解你的问题陈述。