gpt4 book ai didi

python - 在单个字符串中打印 pandas 行(jupyter 笔记本)

转载 作者:行者123 更新时间:2023-11-30 22:14:17 25 4
gpt4 key购买 nike

这段代码:

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
print(df.loc[0], df.loc[1])

给出下一个输出:

col1    1
col2 3
Name: 0, dtype: int64 col1 2
col2 4
Name: 1, dtype: int64

但我想要类似的东西(因为我想在一个循环中从两个不同的数据帧打印很多行对):

1 3, 2 4

请注意,我希望在单行中查看输出。

最佳答案

使用valuesSeries转换为1d numpy array:

print(df.loc[0].values, df.loc[1].values)
[1 3] [2 4]

或转换为列表:

print(df.loc[0].values.tolist(), df.loc[1].values.tolist())
[1, 3] [2, 4]

关于python - 在单个字符串中打印 pandas 行(jupyter 笔记本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50566218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com