[英]Split into columns
I want to make 1500 rows x 3 columns data from below output.我想从下面的输出中制作 1500 行 x 3 列的数据。 First [0,1,2] columns there was some information and I deleted them by using .drop .
首先 [0,1,2] 列有一些信息,我使用 .drop 删除了它们。 At this point, I have difficulty about dividing them to the columns.
在这一点上,我很难将它们分成列。 Could you help me?
你可以帮帮我吗? Thanks!
谢谢!
excel_book_1 ='air-accumulation-c5-v2.csv'
df_1 = pd.read_csv(excel_book_1)
df_1.drop([0,1,2], inplace=True)
df_1.columns = range(df_1.shape[1])
df_1
0
3 1 0 0.01
4 2 0 0.02
5 3 0 0.03
6 4 0 0.04
7 5 0 0.05
... ...
1498 1496 5.036537684491391e-09 14.96
1499 1497 5.036727502175594e-09 14.97
1500 1498 5.036981901795193e-09 14.98
1501 1499 5.037296660302418e-09 14.99
1502 1500 5.037647012557997e-09 15
1500 rows × 1 columns
If columns should be split by spacebars(whitespace) you can do this:如果列应该由空格键(空格)分割,你可以这样做:
df['0'].str.split(expand=True)
['0']
being the name/index of the column you want to split ['0']
是要拆分的列的名称/索引
if you need to split by some other character you can do it like this:如果您需要按其他字符拆分,您可以这样做:
df[['1','2']] = df['0'].str.split("separator",expand=True,)
you can use any "separator".您可以使用任何“分隔符”。 In you case it will look like this:
在你的情况下,它看起来像这样:
df[['1','2','3']] = df['0'].str.split(" ", expand=True)
df['0'].str.split(' ',expand=True)
If your csv file looks like:如果您的 csv 文件如下所示:
blah blah blah blah
blah blah blah blah
blah blah blah blah
1 0 0.01
2 0 0.02
3 0 0.03
4 0 0.04
5 0 0.05
You can use pd.read_csv
like that:您可以像这样使用
pd.read_csv
:
>>> pd.read_csv(excel_book_1, sep=' ', skiprows=3, header=None)
0 1 2
0 1 0 0.01
1 2 0 0.02
2 3 0 0.03
3 4 0 0.04
4 5 0 0.05
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.