数据分析-day04-pandas-dataFrame中查询数据与修改赋值修改语句(r=df[df["z"]==6] & df["z"]=6)

本文通过一个Python脚本示例介绍了如何使用Pandas库进行DataFrame的基本操作,包括创建DataFrame,筛选特定条件的数据,以及修改DataFrame中的列数据。
#!usr/bin/env python
#-*- coding:utf-8 _*-
'''
@author:Administrator
@file: pandas_dataframe_compare_demo.py
@time: 2020-01-05 上午 10:19
'''
import pandas as pd;
import numpy as np
df=pd.DataFrame(np.arange(12).reshape(3,4),columns=(list("xyzd")));
print("#查看所有数据")
print(df)
#查看z列的数据,值为6的数据
print("#查看z列的数据,值为6的数据")
r=df[df["z"]==6]
print(r)
#修改z列的数据,赋值为6
print("#修改z列的数据,赋值为6")
df["z"]=6;
print(df)

生成行为序列特征... 预处理失败: Length mismatch: Expected axis has 1 elements, new values have 6 elements C:\Users\Lenovo\AppData\Local\Temp\ipykernel_20948\3356220079.py:91: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method. The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy. For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object. user_df['user_geohash'].fillna('unknown', inplace=True) C:\Users\Lenovo\AppData\Local\Temp\ipykernel_20948\3356220079.py:91: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy user_df['user_geohash'].fillna('unknown', inplace=True) Traceback (most recent call last): File "C:\Users\Lenovo\AppData\Local\Temp\ipykernel_20948\3356220079.py", line 215, in preprocess_data user_df = generate_behavior_features(user_df) File "C:\Users\Lenovo\AppData\Local\Temp\ipykernel_20948\3356220079.py", line 147, in generate_behavior_features user_item_stats.columns = ['user_id', 'item_id', 'pv', 'cart', 'fav', 'buy'] ^^^^^^^^^^^^^^^^^^^^^^^ File "E:\python\Lib\site-packages\pandas\core\generic.py", line 6335, in __setattr__ return object.__setattr__(self, name, value) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__ File "E:\python\Lib\site-packages\pandas\core\generic.py", line 817, in _set_axis self._mgr.set_axis(axis, labels) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "E:\python\Lib\site-packages\pandas\core\internals\managers.py", line 238, in set_axis self._validate_set_axis(axis, new_labels) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^ File "E:\python\Lib\site-packages\pandas\core\internals\base.py", line 98, in _validate_set_axis raise ValueError( ...<2 lines>... ) ValueError: Length mismatch: Expected axis has 1 elements, new values have 6 elements
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值