如何将 pandas 数据框拆分为多列 [英] How to split a pandas dataframe into multiple columns
本文介绍了如何将 pandas 数据框拆分为多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码:
import pandas as pd
with open('data/training_Origional.csv', 'r') as f:
data2 = pd.read_csv(f)
#Col_Names = list(data2.columns.values)
# data2 = pd.DataFrame(data2.row.str.split('\t', 1).tolist(), columns=Col_Names)
print(data2)
# print(Col_Names)
我想读取一个CSV文件,然后对列值执行操作.当我按原样运行此代码时,会得到以下输出:
I would like to read in a CSV file and then preform operations on the column values. When I run this code as is I get this kind of output:
0 100000\t138.47\t51.655\t97.827\t27.98\t0.9\t1...
1 100001\t160.937\t68.768\t103.235\t48.146\t-999...
2 100002\t-999\t162.172\t125.953\t35.635\t-999\t...
3 100003\t143.905\t81.417\t80.943\t0.414\t-999\t...
我想使用"\ t"作为分隔符将一列分成许多列,但是当我取消注释注释的代码行时,我的代码将不起作用.有没有一种简单的方法可以在熊猫数据框中拆分"列?
I would like to separate out the one column into many columns using the "\t" as a delimiter, but when I uncomment the commented lines of code, my code doesn't work. Is there a simpler way of "splitting" a column in a pandas dataframe?
推荐答案
您需要为"csv"指定分隔符:
You need to specify the separator for your "csv":
pd.read_csv('data/training_Origional.csv', sep='\t')
这篇关于如何将 pandas 数据框拆分为多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文