ash*_*him 5 python list dataframe pandas
我有一个像这样的数据框:
0 1 2 3 4 5 6 7 8 class
0 0.4444 0.0000 0.0000 0.0000 0.1111 0.0000 0.2222 0.0000 0.0000 0
1 0.4444 0.3333 0.3333 0.4444 0.6667 1.0000 0.2222 0.1111 0.0000 0
2 0.2222 0.0000 0.0000 0.0000 0.1111 0.1111 0.2222 0.0000 0.0000 0
Run Code Online (Sandbox Code Playgroud)
我希望将其打印在控制台上,没有换行符,也没有列名:
0.4444,0.0000,0.0000,0.0000,0.1111,0.0000,0.2222,0.0000,0.0000,0
0.4444,0.3333,0.3333,0.4444,0.6667,1.0000,0.2222,0.1111,0.0000,0
0.2222,0.0000,0.0000,0.0000,0.1111,0.1111,0.2222,0.0000,0.0000,0
..
Run Code Online (Sandbox Code Playgroud)
总共有 360 行。
我是熊猫新手,不知道如何实现这一点。我尝试将 pandas 转换为 numpy 数组,列出并尝试打印,print(data, end='')但没有一个对我有用。
如果有人遇到类似的问题,请使用df_to_csv和解决sys.stdout:
df.to_csv(sys.stdout, header=False, index=False, float_format='%.4f')
df.to_string(index=False, header=False)
Run Code Online (Sandbox Code Playgroud)
这就是说,将数据帧转换为字符串,但没有索引标签,也没有列(标题)标签。
我尝试这样做,我可以找到这段代码:
def print_df(df):
# This code delete index and columns names
df_ = df.copy()
df_.columns = ['', '', '', '']
df_.index = ['' for _ in range(len(df))]
print(df_)
print_df(df)
Run Code Online (Sandbox Code Playgroud)
我发现的其他方式,但列格式的值之间没有正确的空格:
[print(*row, sep=', ') for row in df.values.tolist()];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19772 次 |
| 最近记录: |