Python pandas 数据框中整列的子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36505847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
substring of an entire column in pandas dataframe
提问by thenakulchawla
I have a pandas dataframe "df". In this dataframe I have multiple columns, one of which I have to substring. Lets say the column name is "col". I can run a "for" loop like below and substring the column:
我有一个熊猫数据框“df”。在这个数据框中,我有多个列,我必须对其中一列进行子串。假设列名是“col”。我可以运行如下所示的“for”循环并对列进行子字符串化:
for i in range(0,len(df)):
df.iloc[i].col = df.iloc[i].col[:9]
But I wanted to know, if there is an option where I don't have to use a "for" loop, and do it directly using an attribute.I have huge amount of data, and if I do this, the data will take a very long time process.
但我想知道,如果有一个选项,我不必使用“for”循环,而是直接使用属性来做。我有大量的数据,如果我这样做,数据将需要一个很长的过程。
回答by ayhan
回答by Radiumcola
I needed to convert a single column of strings of form nn.n% to float. I needed to remove the % from the element in each row. The attend data frame has two columns.
我需要将一列形式为 nn.n% 的字符串转换为浮点数。我需要从每一行的元素中删除 %。出席数据框有两列。
attend.iloc[:,1:2]=attend.iloc[:,1:2].applymap(lambda x: float(x[:-1]))
Its an extenstion to the original answer. In my case it takes a dataframe and applies a function to each value in a specific column. The function removes the last character and converts the remaining string to float.
它是原始答案的扩展。在我的情况下,它需要一个数据框并将一个函数应用于特定列中的每个值。该函数删除最后一个字符并将剩余的字符串转换为浮点数。