我有数据框:
Values Values2
1,2,3,4 0,2,3
2,1,0,6 0,0,0
9,8,7,6 1,0,1
Run Code Online (Sandbox Code Playgroud)
我想创建列表列表.我这样做是这样的:
df[['Values']].values.tolist()
Run Code Online (Sandbox Code Playgroud)
在输出中得到:
[['1,2,3,4'],
['2,1,0,6'],
['9,8,7,6']]
Run Code Online (Sandbox Code Playgroud)
这是一个字符串,但我需要一个像这样的整数列表:
[[1,2,3,4],
[2,1,0,6],
[9,8,7,6]]
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个file.dat,看起来像:
id | user_id | venue_id | latitude | longitude | created_at
---------+---------+----------+-----------+-----------+-----------------
984301 |2041916 |5222 | | |2012-04-21 17:39:01
984222 |15824 |5222 |38.8951118 |-77.0363658|2012-04-21 17:43:47
984315 |1764391 |5222 | | |2012-04-21 17:37:18
984234 |44652 |5222 |33.800745 |-84.41052 | 2012-04-21 17:43:43
Run Code Online (Sandbox Code Playgroud)
我需要获取带有删除的空纬度和经度行的csv文件,例如:
id,user_id,venue_id,latitude,longitude,created_at
984222,15824,5222,38.8951118,-77.0363658,2012-04-21T17:43:47
984234,44652,5222,33.800745,-84.41052,2012-04-21T17:43:43
984291,105054,5222,45.5234515,-122.6762071,2012-04-21T17:39:22
Run Code Online (Sandbox Code Playgroud)
我尝试使用下一个代码:
with open('file.dat', 'r') as input_file:
lines = input_file.readlines()
newLines = []
for line in lines:
newLine = line.strip('|').split()
newLines.append(newLine)
with open('file.csv', 'w') as output_file:
file_writer = csv.writer(output_file)
file_writer.writerows(newLines)
Run Code Online (Sandbox Code Playgroud)
但是我得到一个带有"|"的csv文件 符号和空纬度/经度行.哪里出错?一般来说,我需要在DateFrame中使用生成的csv文件,因此可能有一些方法可以减少操作次数.
我有一个数组:
X = [[2, 2, 2],
[3, 3, 3],
[4, 4, 4]]
Run Code Online (Sandbox Code Playgroud)
我需要在 numpy 数组中添加额外的列,并使用 hstack 和 reshape 填充它。像那样:
X = [[2, 2, 2, 1],
[3, 3, 3, 1],
[4, 4, 4, 1]]
Run Code Online (Sandbox Code Playgroud)
我所做的:
X = np.hstack(X, np.ones(X.reshape(X, (2,3))))
Run Code Online (Sandbox Code Playgroud)
并得到一个错误:
TypeError: only length-1 arrays can be converted to Python scalars
Run Code Online (Sandbox Code Playgroud)
有什么问题?我做错了什么?