Pandas 数据分割
2020-02-08 本文已影响0人
Noza_ea8f
表
image.png
Code
df = pd.read_excel(io='exls/data_splitting.xlsx', sheet_name='Sheet1', dtype=str, index_col='ID')
df_ = df['data'].str.split('.', expand=True)
print(df_)
dtype=str,如果不设置,数据会被识别成数字,无法拆分;
split('.', expand=True),expand=True意在把一列数据直接分割成两列,
否则数据为一列的列表格式;
Output
0 1
ID
1 -2 087552
2 -2 087617
3 -2 086667
4 -2 087578
5 -2 087548
6 -2 08752
7 -2 08756
8 -2 087567
9 -2 087548
10 -2 087548
添加列
import pandas as pd
df = pd.read_excel(io='exls/data_splitting.xlsx', sheet_name='Sheet1', dtype=str, index_col='ID')
df_ = df['data'].str.split('.', expand=True)
df['New_1'] = df_[0]
df['New_2'] = df_[1]
print(df)
Output
data New_1 New_2
ID
1 -2.087552 -2 087552
2 -2.087617 -2 087617
3 -2.086667 -2 086667
4 -2.087578 -2 087578
5 -2.087548 -2 087548
6 -2.08752 -2 08752
7 -2.08756 -2 08756