简体   繁体   中英

Split into columns

I want to make 1500 rows x 3 columns data from below output. First [0,1,2] columns there was some information and I deleted them by using .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

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:

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(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

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