![](/img/trans.png)
[英]How to correctly read csv in Pandas while changing the names of the columns
[英]How to read pandas efficiently, with array of columns names
df = pd.DataFrame({"col_a": [1,2,3], "col_b": [5,4,0], "col_c": [9,7,6])
cols = [["col_a", "col_b"],["col_c", "col_b"],["col_a", "col_b"]]
#expected output:[[1,5],[7,4],[3,0]]
我知道这可以使用列表理解来实现,寻找更有效的方法,因为我有超过一百万条记录
您忘记提供的列表理解:
In [27]: [row[1][col].to_list() for row, col in zip(df.iterrows(), cols)]
Out[27]: [[1, 5], [7, 4], [3, 0]]
我认为如果不迭代cols
变量,您将无法做到这一点。 尝试这个 -
[df.loc[i,j].tolist() for i,j in enumerate(cols)]
[[1, 5], [7, 4], [3, 0]]
你可以 map 你的标签到索引然后take_along_axis
d = {c: i for i,c in enumerate(df.columns)}
idx = pd.DataFrame(cols).replace(d).to_numpy()
#array([[0, 1],
# [2, 1],
# [0, 1]])
np.take_along_axis(df.to_numpy(), idx, axis=1)
#array([[1, 5],
# [7, 4],
# [3, 0]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.