一个begineer如果数值之后的字符是一个标签,你没事,所有你需要的
import io # using io.StringIO for demonstration
import pandas as pd
ratings = "-1\tThis movie really sucks.\n-1\tRun colored water through
a reflux condenser and call it a science movie?\n+1\tJust another zombie flick? You'll be surprised!"
df = pd.read_csv(io.StringIO(ratings), sep='\t',
header=None, names=['change', 'rating'])
- 传递
header=None
可以确保第一行是解释为数据。
- 传递
names=['change', 'rating']
提供了一些(合理的)列标题。
当然,该字符不是一个选项卡:D。
import io # using io.string
import pandas as pd
ratings = "-1 This movie really sucks.\n-1 Run colored water through a
reflux condenser and call it a science movie?\n+1 Just another zombie
flick? You'll be surprised!"
df = pd.read_csv(io.StringIO(ratings), sep='\t',
header=None, names=['stuff'])
df['change'], df['rating'] = df.stuff.str[:3], df.stuff.str[3:]
df.drop('stuff', axis=1)
一个可行的选择是将整个评分读作一个临时列,拆分字符串,将其分配到两列并最终删除临时列。
请将您的数据样本作为文本发布,而不是图片。 –