[英]how to read text file into array into pandas dataframe
I want to create a new column that reads a file name from the column : df['Filename'] applies np.genfromtext and then puts the resulting array in a new column called Scan. 我想创建一个新列,以从该列读取文件名:df ['Filename']应用np.genfromtext,然后将所得数组放入一个名为Scan的新列中。
d = {'Filename':['G924310X.txt','G924330X.txt','G924340X.txt'],
'Longitude':[92.4,92.4,92.4],
'Latitude':[-31.0,-33.0,-34.0]}
df = pd.DataFrame(data = d)
def f(x):
return np.genfromtxt(x , delimiter=', ')
df['Scan'] = df.apply(lambda x: f(df['Filename']), axis = 1)
I get this error, TypeError: ("Can't convert 'bytes' object to str implicitly", 'occurred at index 0') 我收到此错误,TypeError :(“无法将'bytes'对象隐式转换为str”,“发生在索引0”)
Is there a way to do this? 有没有办法做到这一点?
I believe you should change 我相信你应该改变
df['Scan'] = df.apply(lambda x: f(df['Filename']), axis = 1)
for 对于
df['Scan'] = df.apply(lambda x: f(x['Filename'].item()), axis = 1)
Or maybe 或者可能
df['Scan'] = df.Filename.apply(f, axis = 1)
Otherwise apply
will always call the function with your whole series for every row. 否则,
apply
将始终对每一行调用整个系列的函数。 The .item()
func just pops out the unique elem in the series. .item()
函数仅弹出该系列中的唯一元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.