Python-dataframe操作
2019-04-16 本文已影响0人
微雨旧时歌丶
- 某列单元格元素的替换
df=df.replace({'name':s},alt_name) # 'name'就是df中的‘name’列,将字符串s替换为字符串alt_name,单元格匹配
- 字典变DataFrame
df = pd.DataFrame(list(dict_FileCountry.items()), columns=['fileName', 'country'])
- 字典变dataframe 2
df_locations = pd.read_csv('locations.csv',dtype = 'str')
dict_locations = df_locations.set_index('name').T.to_dict() # 转化为字典
- 任取两列变字典
countries_GDP = dict(zip(df_GDP['Country Name'],df_GDP[2018]))
- 变回字典
# 读时区信息
utc = pd.read_excel('AssistData/timezone.xls')
dict_utc = utc.set_index('Country').T.to_dict('list')
utc = {}
for k in dict_utc:
utc[k] = dict_utc[k][0]
dict_utc = utc
- 变矩阵
df = pd.read_excel(in_file,dtype = 'str')
a=df.values # 矩阵形式,[行][列],或者[行,列] 从0开始
# 取一列: a[:,j]
# 取一行:a[i]
nrows = a.shape[0] #行数
ncols = a.shape[1] # 列数
df_new = pd.DataFrame(a) # 变回dataframe