我的数据集如下所示。我正在尝试读取“每”列中的数字,而不读取“%”符号。作为python的初学者,我想知道我们是否可以在python中这样做。另外,如果您能提供很好的解释!
State Year per
A 1990 6.10%
A 1989 4.50%
B 1990 3.4%
B 1989 1.25%
预先感谢
答案 0 :(得分:1)
如果它是一个csv文件,这应该有所帮助(否则可能会有另一种获取数据帧的方法):
import pandas as pd
data = pd.read_csv("somefile.csv")
data["per"] = data["per"].str.replace("%", "").to_numeric()
答案 1 :(得分:0)
您的文件类型与此无关,不需要任何模块。它的工作原理是每一行都走到最后一个单词。然后,它会拆分百分比并删除百分比符号。
def readFile(filename):
percents = []
with open (filename,"r") as f:
for row in f:#for each line, we remove the first one late
splitRow = row.split()[-1]# spliting the elements by word, we want the last one only
percent = splitRow
percent = percent.split("%")[0]#removing the percent
percents.append(percent)#if you want it as an number instead of a string do percents.append(float(percent))
percents = percents[1:] # removes the header "per"
return percents