df['foo'] = 100 # 增加一列foo,所有值都是100df['foo'] = df.Q1 + df.Q2 # 新列为两列相加df['foo'] = df['Q1'] + df['Q2'] # 同上# 把所有为数字的值加起来df['total'] =df.select_dtypes(include=['int']).sum(1)df['total'] =df.loc[...
sep =';',# 文本分隔符,默认是逗号header =True,# 是否保存列索引index =True)# 是否保存行索引,保存行索引,文件被加载时,默认行索引会作为一列# 这里一般 index = False 不设索引会比较好# 读取数据 - read_csvdata1 = pd.read_csv('data/salary.csv', sep =';',# 默认是逗号 - 不是逗号分割要...
(self, key, value) 1284 ) 1285 1286 check_dict_or_set_indexers(key) 1287 key = com.apply_if_callable(key, self) -> 1288 cacher_needs_updating = self._check_is_chained_assignment_possible() 1289 1290 if key is Ellipsis: 1291 key = slice(None) ~/work/pandas/pandas/pandas/core/seri...
info()) #代码运行结果: <class 'pandas.core.frame.DataFrame'> RangeIndex: 249 entries, 0 to 248 Data columns (total 8 columns): # Column Non-Null Count Dtype --- --- --- --- 0 电影名称 249 non-null object 1 上映年份 249 non-null int64 2 导演249 non-null object 3 类型249 no...
select_dtypes() 的作用是,基于 dtypes 的列返回数据帧列的一个子集。这个函数的参数可设置为包含所有拥有特定数据类型的列,亦或者设置为排除具有特定数据类型的列。 # We'll use the same dataframe that we used for read_csvframex = df.select_dtypes(include="...
data.tail():返回data的后几行数据,默认为后五行,需要后十行则data.tail(10) (2) unique和nunique data['column'].nunique():显示有多少个唯一值 data['column'].unique():显示所有的唯一值 (3) count和value_counts data['column'].count():返回非缺失值元素个数 ...
drinks = pd.read_csv('data/drinks.csv') # 选择所有数值型的列 drinks.select_dtypes(include=['number']).head() # 选择所有字符型的列 drinks.select_dtypes(include=['object']).head() drinks.select_dtypes(include=['number','object','category','datetime']).head() #用 exclude 关键字排除指...
df = pd.read_csv('data.csv') #从 Excel 文件中读取数据 df = pd.read_excel('data.xlsx') #从 SQL 数据库中读取数据 import sqlite3 conn = sqlite3.connect('database.db') df = pd.read_sql('SELECT * FROM table_name', conn) #从 JSON 字符串中读取数据 json_string = '{"name": "...
df.loc[row_label, column_label] 4. 选取连续的行或者列的数据(切片) df.loc[row1_label:row2_label,col1_label,col2_label] 这个方法用于选取多行多列连续的数据。 下面是一个使用 df.loc[] 方法的示例代码 import pandas as pd data = {'Name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'Age...
# np.where(condition, value if condition is true, value if condition is false) df['hasimage'] = np.where(df['photos']!= '[]', True, False) 多条件:使用一个名为np.select()的函数,给它提供两个参数:一个是条件,另一个是对应的等级列表。 # create a list of our conditions conditions ...