简体   繁体   English

当我的列 header 的名称中有 \ 时,如何将 dataframe 拆分为多个列?

[英]How can I split a dataframe into multiple columns when my column header has \ in the name?

I have a dataframe called ratings.我有一个称为评级的 dataframe。 It is a single column, named "tconst\taverageRating\tnumVotes", although it needs to be split into 3, separated by "\".它是一个单独的列,命名为“tconst\taverageRating\tnumVotes”,尽管它需要拆分为 3,用“\”分隔。

I understand that this statement can be used: ratings[['tconst','taverageRating','tnumVotes']] = ratings.???.str.split("\",expand=True,)我理解可以使用这个语句: ratings[['tconst','taverageRating','tnumVotes']] = ratings.???.str.split("\",expand=True,)

However, when I replace???但是,当我更换??? with the actual column name, I get an error.使用实际的列名,我得到一个错误。 I believe this is because of the \s in the column header.我相信这是因为 header 列中的 \s。

Any help would be greatly appreciated.任何帮助将不胜感激。

Are you sure you are reading the data in correctly?您确定您正在正确读取数据吗? Looking at the header names, it looks likely that your data is actually \t ie tab separated (so, \t and numVotes make sense separately).查看 header 名称,看起来您的数据实际上是\ttab分隔的(因此, \tnumVotes分别有意义)。 In that case you should read your data like this:在这种情况下,您应该像这样读取您的数据:

pd.read_csv('http://language.media.mit.edu/data/public/unesco_langlang_20120722_iso639-3.txt', sep='\t') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM