python pandas.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python pandas.py相关的知识,希望对你有一定的参考价值。

# 循环DataFrame
for index, row in df.iterrows()

# 修改字段类型
source_prices['avg_price'] = source_prices['avg_price'].astype('int')

# df1.dtypes 查看df属性类型
df1['house_toward'] = df1['house_toward'].astype('str', errors='ignore')
data2['house_toward'] = data2['house_toward'].astype('str', errors='ignore')

# DataFrame执行groupby聚合操作后,如何继续保持DataFrame对象而不变成Series对象
http://blog.csdn.net/u014252563/article/details/70835271
用groupby聚合后,之前的dataframe对象变成了series对象,聚合的字段变成了索引index,导致获取这些字段时很麻烦,后面发现reset_index()这个函数,完美的解决了我的需求。

# pandas agg groupby 聚合示例
def pd_group(city_en):
    now = pendulum.now()
    d = int(now.format('%Y%m%d'))
    t = now.int_timestamp
    db_str = zhuge_db_utils.get_city_dbstr(city_en)
    all = mongo_conn[db_str].cityarea2_price.find()  # .limit(10)
    df = pd.DataFrame(list(all))
    df['yymm'] = df['date'].map(lambda x: int(str(x)[0:6]))
    df['year'] = df['date'].map(lambda x: int(str(x)[0:4]))
    df['month'] = df['date'].map(lambda x: int(str(x)[4:6]))
    f = {'price': 'mean', 'count_gov': 'mean', 'cityarea_name': 'last',
         'cityarea2_name': 'last', 'cityarea_id': 'last', 'year': 'last', 'month': 'last'}
    df1 = df.groupby(['cityarea2_id', 'yymm']).agg(f)
    df1 = df1.reset_index()
    df1['date'] = d
    df1['time'] = t
    df1.rename(columns={'price': 'avg_price'}, inplace=True)
    df1['avg_price'] = df1['avg_price'].astype('int')
    df1['count_gov'] = df1['count_gov'].astype('int')
    df1 = df1.fillna({'cityarea_name': '', 'cityarea2_name': ''})
    return df1
  

以上是关于python pandas.py的主要内容,如果未能解决你的问题,请参考以下文章

python Pandas.py

python pandas.py

python test_pandas.py

python test_pandas.py

python pandas_snippets.py

python python-pandas-reading-from-files.py