How to work with printed Pandas Dataframe output?

Viewed 47

What's the easies way to make a pandas dataframe from printed pandas dataframe output?

Context: Many questions on StackOverflow are asked together with data looking like this:

index   count1   count2   diff
A       1        0        1
B       1        2        -1
C       1        2        -1
D       0        2        -2

I want to run the persons (in this case hypothetical) code with the data. What's the fastest/easiest way to load this data into Pandas?

1 Answers

Before I learned today about read_clipboard() from @Brendan's comment above, I used to do this:

text = """index   count1   count2   diff
A       1        0        1
B       1        2        -1
C       1        2        -1
D       0        2        -2"""
data = np.array(list(text.split())).reshape(5, 4)
df = pd.DataFrame(data[1:], columns=data[0])
Related