我目前正在使用这段代码:-
print(df.to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))
通过这个我得到了文本文件中的所有列,但我只想要一个列。有人知道我该怎么做吗? 提前致谢
请您参考如下方法:
只需子选择感兴趣的列:
print(df[col_of_interest].to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))
通过执行 df[col_of_interest],您只选择将返回 Series 的单个列,如果您想要数据框使用双方括号:
print(df[[col_of_interest]].to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a'))
您还可以将单个列表项传递给参数 columns:
print(df.to_csv(r'tweets.txt', header=None, index=None, sep=' ', mode='a', columns=['col_of_interest']))
参见 docs
