将文本表转换为Pandas DataFrame [英] Convert text table to pandas dataframe

查看:475
本文介绍了将文本表转换为Pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候,当我试图回答有关Stackoverflow的问题时,该问题都包含一个表,为了处理该表,我必须将其转换为pandas数据框.例如,在这个问题中:

Many times when I'm trying to answer questions on Stackoverflow, the question contains a table, which I have to convert to a pandas dataframe in order to process. for example, in this question:

http://stackoverflow.com/questions/43172116/pandas-count-some-value-in-all-columns

我的问题是,有没有一种更快的方法可以将其转换为数据框而不是编写:

My question is, is there a faster way to convert it to a dataframe rather than writing:

df=pd.DataFrame({'graph':[1,2,3,4,5,6],
0:['blue','blue','red','red','blue','blue'],
1:['blue','blue','red','blue','red','blue'],
2:['blue','blue','blue','red','blue','blue'],
3:['blue','blue','blue','red','red','blue'],
4:['blue','blue','red','blue','red','blue']})

假设我可以复制文本:

graph   0       1       2       3       4
1       blue    blue    blue    blue    blue
2       blue    blue    blue    blue    blue
3       blue    red     blue    blue    red
4       red     blue    red     red     blue
5       red     red     blue    red     red
6       blue    blue    blue    blue    blue

推荐答案

确保所需的数据集在剪贴板中,并使用

Make sure the desired data set is in clipboard and use pd.read_clipboard() method.

逐步:

  1. 标记所需的数据集
  2. Ctrl + C (对于MS Windows)
  3. 执行:df = pd.read_clipboard()
  1. mark desired data set
  2. press Ctrl+C (for MS Windows)
  3. execute: df = pd.read_clipboard()


In [40]: df = pd.read_clipboard()

In [41]: df
Out[41]:
   graph     0     1     2     3     4
0      1  blue  blue  blue  blue  blue
1      2  blue  blue  blue  blue  blue
2      3  blue   red  blue  blue   red
3      4   red  blue   red   red  blue
4      5   red   red  blue   red   red
5      6  blue  blue  blue  blue  blue

这篇关于将文本表转换为Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆