将DataFrame值转换为int,添加它们并创建带有结果的新列? [英] Converting DataFrame values to int, adding them and create new column with result?
问题描述
我有一个非常大的字符串数字数据框,例如:
I have a very large dataframe of string numbers, something like for example:
a,b,c
"1","2","3"
"4","5","6"
"7","8","9"
我想创建一个新列d
并添加a + c
,所以最终结果将是:
And I want to create a new column d
with the addition of a + c
so the end result would be:
a,b,c,d
1,2,3,4
4,5,6,10
7,8,9,16
我仍在尝试仅将a + c
的列转换为字符串,但是我不知道如何将它们加在一起并创建结果的新列.请帮助解决最后一个问题!
I'm still trying to convert just the columns of a + c
to strings, but I have no idea how I'll add them together and create a new column of the result. Please help with this last problem!
推荐答案
我认为read_csv
将列转换为整数.
In my opinion read_csv
convert columns to integers.
因此使用:
df = pd.read_csv(file)
df['d'] = df['a'] + df['c']
但是如果失败,则尝试转换为整数或浮点数:
But if failed, then try convert to integer or floats:
df = pd.read_csv(file)
df['d'] = df['a'].astype(int) + df['c'].astype(int)
#floats
#df['d'] = df['a'].astype(float) + df['c'].astype(float)
如果数字之间也可能存在一些字符串,则可以将问题值转换为NaN
s并求和:
If there are also some strings between numeric is possible convert problems values to NaN
s and sum:
df = pd.read_csv(file)
df['d'] = pd.to_numeric(df['a'], errors='coerce') + pd.to_numeric(df['c'], errors='coerce')
这篇关于将DataFrame值转换为int,添加它们并创建带有结果的新列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!