import pandas as pd
# 按文件名读取整个文件
data = pd.read_csv("churn-bigml-80.csv")
data.head() # 展示前几行数据,通常为展示前5行
# 按列标题读取每列数据
feature_name = ['State', 'Account length', 'Area code', 'International plan', 'Voice mail plan',
'Number vmail messages', 'Total day minutes', 'Total day calls', 'Total day charge',
'Total eve minutes', 'Total eve calls', 'Total eve charge', 'Total night minutes',
'Total night calls', 'Total night charge', 'Total intl minutes', 'Total intl calls',
'Total intl charge', 'Customer service calls']
target_name = ['Churn']
x_data = pd.read_csv("churn-bigml-80.csv", encoding='utf-8', usecols=feature_name)
y_data = pd.read_csv("churn-bigml-80.csv", encoding='utf-8', usecols=target_name)
print(x_data)
若不传入数据r来指定返回行数,则默认返回前5行数据。
若不传入数据r来指定返回行数,则默认返回最后5行数据。
head_data = data.head()
tail_data = data.tail(3)
print('Default first few lines of data:\n', head_data)
print('\nLast 3 lines of data:\n', tail_data)
s = data.shape
print(s)
运行结果:
(2666, 20)
ty = data.dtypes
print(ty)
value = data.values
ty = type(value)
print(ty)
print(value)
value = data.nunique()
num_list = data.nunique().tolist()
key_list = data.nunique().keys().tolist()
print('number of unique: \n', value)
print('\nkey list: \n', key_list)
print('\nonly value list: \n', num_list)
运行结果:
可以看出,单独代用dataframe的属性,返回的数据类型并不是list,因此可以通过加上.tolist()来将数据转化成list,便于后续操作。
row_index = data.index
ty = type(row_index)
row_index_list = data.index.tolist()
print('rows index: \n', row_index)
print('\ntype: \n', ty)
print('\nlist: \n', row_index_list)
col_index = data.columns
ty = type(col_index)
col_index_list = data.columns.tolist()
print('rows index: \n', col_index)
print('\ntype: \n', ty)
print('\nlist: \n', col_index_list)
运行结果:
这里是引用
x1 = data.iloc[0, 0]
print(x1)
运行结果:
KS
x1 = data.loc[0, 'State']
print(x1)
运行结果:
KS