如何移除Pandas DataFrame的行索引与列标签?
Hey there! Let's get your Pandas DataFrame to output just the raw data—no row indices or column labels included. Here are a few simple, effective methods to achieve exactly what you're looking for:
Method 1: Use to_string() with custom parameters
The to_string() method lets you customize how your DataFrame is printed. By setting index=False and header=False, you'll suppress both the row indices and column labels entirely:
import pandas as pd l = [[1,2,3],[4,5,6],[7,8,9]] New_dataframe = pd.DataFrame(l) # Print without index and headers print(New_dataframe.to_string(index=False, header=False))
This will output exactly the format you want:
1 2 3 4 5 6 7 8 9
Method 2: Use to_csv() for space-separated output
If you prefer a CSV-style approach (with spaces as separators), you can use to_csv() and disable indices/headers to get clean, unlabeled data:
print(New_dataframe.to_csv(sep=' ', index=False, header=False))
This produces the same clean output as above. Note: This method adds a trailing newline, but it's usually negligible for most use cases.
Method 3: Manual row iteration (for full control)
If you want complete control over how each line is formatted, you can iterate over the DataFrame's values directly, convert each row to a string, and print it:
for row in New_dataframe.values: print(' '.join(map(str, row)))
This approach is great if you need to tweak formatting further—like changing separators or adding custom text to individual rows.
内容的提问来源于stack exchange,提问作者Shubham Kuse

