简体   繁体   中英

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

I have a dataframe called ratings. It is a single column, named "tconst\taverageRating\tnumVotes", although it needs to be split into 3, separated by "\".

I understand that this statement can be used: 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.

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). 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') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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