python对dataframe进行操作? python对dataframe列里的元素进行修改?

python\u600e\u4e48\u5bf9dataframe\u8fdb\u884c\u64cd\u4f5c

\u7528pandas\u4e2d\u7684DataFrame\u65f6\u9009\u53d6\u884c\u6216\u5217\uff1a
import numpy as npimport pandas as pdfrom pandas import Sereis, DataFrameser = Series(np.arange(3.))data = DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('wxyz'))data['w'] #\u9009\u62e9\u8868\u683c\u4e2d\u7684'w'\u5217\uff0c\u4f7f\u7528\u7c7b\u5b57\u5178\u5c5e\u6027,\u8fd4\u56de\u7684\u662fSeries\u7c7b\u578bdata.w #\u9009\u62e9\u8868\u683c\u4e2d\u7684'w'\u5217\uff0c\u4f7f\u7528\u70b9\u5c5e\u6027,\u8fd4\u56de\u7684\u662fSeries\u7c7b\u578bdata[['w']] #\u9009\u62e9\u8868\u683c\u4e2d\u7684'w'\u5217\uff0c\u8fd4\u56de\u7684\u662fDataFrame\u7c7b\u578bdata[['w','z']] #\u9009\u62e9\u8868\u683c\u4e2d\u7684'w'\u3001'z'\u5217data[0:2] #\u8fd4\u56de\u7b2c1\u884c\u5230\u7b2c2\u884c\u7684\u6240\u6709\u884c\uff0c\u524d\u95ed\u540e\u5f00\uff0c\u5305\u62ec\u524d\u4e0d\u5305\u62ec\u540edata[1:2] #\u8fd4\u56de\u7b2c2\u884c\uff0c\u4ece0\u8ba1\uff0c\u8fd4\u56de\u7684\u662f\u5355\u884c\uff0c\u901a\u8fc7\u6709\u524d\u540e\u503c\u7684\u7d22\u5f15\u5f62\u5f0f\uff0c
#\u5982\u679c\u91c7\u7528data[1]\u5219\u62a5\u9519data.ix[1:2] #\u8fd4\u56de\u7b2c2\u884c\u7684\u7b2c\u4e09\u79cd\u65b9\u6cd5\uff0c\u8fd4\u56de\u7684\u662fDataFrame\uff0c\u8ddfdata[1:2]\u540cdata['a':'b'] #\u5229\u7528index\u503c\u8fdb\u884c\u5207\u7247\uff0c\u8fd4\u56de\u7684\u662f**\u524d\u95ed\u540e\u95ed**\u7684DataFrame,
#\u5373\u672b\u7aef\u662f\u5305\u542b\u7684
data.irow(0) #\u53d6data\u7684\u7b2c\u4e00\u884cdata.icol(0) #\u53d6data\u7684\u7b2c\u4e00\u5217data.head() #\u8fd4\u56dedata\u7684\u524d\u51e0\u884c\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3a\u524d\u4e94\u884c\uff0c\u9700\u8981\u524d\u5341\u884c\u5219data.head(10)data.tail() #\u8fd4\u56dedata\u7684\u540e\u51e0\u884c\u6570\u636e\uff0c\u9ed8\u8ba4\u4e3a\u540e\u4e94\u884c\uff0c\u9700\u8981\u540e\u5341\u884c\u5219data.tail(10)ser.iget_value(0) #\u9009\u53d6ser\u5e8f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2aser.iget_value(-1) #\u9009\u53d6ser\u5e8f\u5217\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\uff0c\u8fd9\u79cd\u8f74\u7d22\u5f15\u5305\u542b\u7d22\u5f15\u5668\u7684series\u4e0d\u80fd\u91c7\u7528ser[-1]\u53bb\u83b7\u53d6\u6700\u540e\u4e00\u4e2a\uff0c\u8fd9\u4f1a\u5f15\u8d77\u6b67\u4e49\u3002data.iloc[-1] #\u9009\u53d6DataFrame\u6700\u540e\u4e00\u884c\uff0c\u8fd4\u56de\u7684\u662fSeriesdata.iloc[-1:] #\u9009\u53d6DataFrame\u6700\u540e\u4e00\u884c\uff0c\u8fd4\u56de\u7684\u662fDataFramedata.loc['a',['w','x']] #\u8fd4\u56de\u2018a\u2019\u884c'w'\u3001'x'\u5217\uff0c\u8fd9\u79cd\u7528\u4e8e\u9009\u53d6\u884c\u7d22\u5f15\u5217\u7d22\u5f15\u5df2\u77e5data.iat[1,1] #\u9009\u53d6\u7b2c\u4e8c\u884c\u7b2c\u4e8c\u5217\uff0c\u7528\u4e8e\u5df2\u77e5\u884c\u3001\u5217\u4f4d\u7f6e\u7684\u9009\u53d6\u3002123456789101112131415161718192021222324252627282930313233343536373839404142

\u4e0b\u9762\u662f\u7b80\u5355\u7684\u4f8b\u5b50\u4f7f\u7528\u9a8c\u8bc1\uff1a
import pandas as pdfrom pandas import Series, DataFrame
import numpy as np

data = DataFrame(np.arange(15).reshape(3,5),index=['one','two','three'],columns=['a','b','c','d','e'])

data
Out[7]:
a b c d eone 0 1 2 3 4two 5 6 7 8 9three 10 11 12 13 14#\u5bf9\u5217\u7684\u64cd\u4f5c\u65b9\u6cd5\u6709\u5982\u4e0b\u51e0\u79cddata.icol(0) #\u9009\u53d6\u7b2c\u4e00\u5217E:\Anaconda2\lib\site-packages\spyder\utils\ipython\start_kernel.py:1: FutureWarning: icol(i) is deprecated. Please use .iloc[:,i] # -*- coding: utf-8 -*-Out[35]:
one 0two 5three 10Name: a, dtype: int32

data['a']
Out[8]:
one 0two 5three 10Name: a, dtype: int32

data.aOut[9]:
one 0two 5three 10Name: a, dtype: int32

data[['a']]
Out[10]:
aone 0two 5three 10data.ix[:,[0,1,2]] #\u4e0d\u77e5\u9053\u5217\u540d\u53ea\u77e5\u9053\u5217\u7684\u4f4d\u7f6e\u65f6Out[13]:
a b cone 0 1 2two 5 6 7three 10 11 12data.ix[1,[0]] #\u9009\u62e9\u7b2c2\u884c\u7b2c1\u5217\u7684\u503cOut[14]:
a 5Name: two, dtype: int32

data.ix[[1,2],[0]] #\u9009\u62e9\u7b2c2,3\u884c\u7b2c1\u5217\u7684\u503cOut[15]:
atwo 5three 10data.ix[1:3,[0,2]] #\u9009\u62e9\u7b2c2-4\u884c\u7b2c1\u30013\u5217\u7684\u503cOut[17]:
a ctwo 5 7three 10 12data.ix[1:2,2:4] #\u9009\u62e9\u7b2c2-3\u884c\uff0c3-5\uff08\u4e0d\u5305\u62ec5\uff09\u5217\u7684\u503cOut[29]:
c dtwo 7 8data.ix[data.a>5,3]
Out[30]:
three 13Name: d, dtype: int32

data.ix[data.b>6,3:4] #\u9009\u62e9'b'\u5217\u4e2d\u5927\u4e8e6\u6240\u5728\u7684\u884c\u4e2d\u7684\u7b2c4\u5217\uff0c\u6709\u70b9\u62d7\u53e3Out[31]:
dthree 13data.ix[data.a>5,2:4] #\u9009\u62e9'a'\u5217\u4e2d\u5927\u4e8e5\u6240\u5728\u7684\u884c\u4e2d\u7684\u7b2c3-5\uff08\u4e0d\u5305\u62ec5\uff09\u5217Out[32]:
c dthree 12 13data.ix[data.a>5,[2,2,2]] #\u9009\u62e9'a'\u5217\u4e2d\u5927\u4e8e5\u6240\u5728\u7684\u884c\u4e2d\u7684\u7b2c2\u5217\u5e76\u91cd\u590d3\u6b21Out[33]:
c c cthree 12 12 12#\u8fd8\u53ef\u4ee5\u884c\u6570\u6216\u5217\u6570\u8ddf\u884c\u540d\u5217\u540d\u6df7\u7740\u7528data.ix[1:3,['a','e']]
Out[24]:
a etwo 5 9three 10 14data.ix['one':'two',[2,1]]
Out[25]:
c bone 2 1two 7 6data.ix[['one','three'],[2,2]]
Out[26]:
c cone 2 2three 12 12data.ix['one':'three',['a','c']]
Out[27]:
a cone 0 2two 5 7three 10 12data.ix[['one','one'],['a','e','d','d','d']]
Out[28]:
a e d d done 0 4 3 3 3one 0 4 3 3 3#\u5bf9\u884c\u7684\u64cd\u4f5c\u6709\u5982\u4e0b\u51e0\u79cd\uff1adata[1:2] #\uff08\u4e0d\u77e5\u9053\u5217\u7d22\u5f15\u65f6\uff09\u9009\u62e9\u7b2c2\u884c\uff0c\u4e0d\u80fd\u7528data[1]\uff0c\u53ef\u4ee5\u7528data.ix[1]Out[18]:
a b c d etwo 5 6 7 8 9data.irow(1) #\u9009\u53d6\u7b2c\u4e8c\u884cOut[36]:
a 5b 6c 7d 8e 9Name: two, dtype: int32

data.ix[1] #\u9009\u62e9\u7b2c2\u884cOut[20]:
a 5b 6c 7d 8e 9Name: two, dtype: int32


data['one':'two'] #\u5f53\u7528\u5df2\u77e5\u7684\u884c\u7d22\u5f15\u65f6\u4e3a\u524d\u95ed\u540e\u95ed\u533a\u95f4\uff0c\u8fd9\u70b9\u4e0e\u5207\u7247\u7a0d\u6709\u4e0d\u540c\u3002Out[22]:
a b c d eone 0 1 2 3 4two 5 6 7 8 9data.ix[1:3] #\u9009\u62e9\u7b2c2\u52304\u884c\uff0c\u4e0d\u5305\u62ec\u7b2c4\u884c\uff0c\u5373\u524d\u95ed\u540e\u5f00\u533a\u95f4\u3002Out[23]:
a b c d etwo 5 6 7 8 9three 10 11 12 13 14data.ix[-1:] #\u53d6DataFrame\u4e2d\u6700\u540e\u4e00\u884c\uff0c\u8fd4\u56de\u7684\u662fDataFrame\u7c7b\u578b,**\u6ce8\u610f**\u8fd9\u79cd\u53d6\u6cd5\u662f\u6709\u4f7f\u7528\u6761\u4ef6\u7684\uff0c\u53ea\u6709\u5f53\u884c\u7d22\u5f15\u4e0d\u662f\u6570\u5b57\u7d22\u5f15\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528\uff0c\u5426\u5219\u53ef\u4ee5\u9009\u7528`data[-1:]`--\u8fd4\u56deDataFrame\u7c7b\u578b\u6216`data.irow(-1)`--\u8fd4\u56deSeries\u7c7b\u578bOut[11]:
a b c d ethree 10 11 12 13 14data[-1:] #\u8ddf\u4e0a\u9762\u4e00\u6837\uff0c\u53d6DataFrame\u4e2d\u6700\u540e\u4e00\u884c\uff0c\u8fd4\u56de\u7684\u662fDataFrame\u7c7b\u578bOut[12]:
a b c d ethree 10 11 12 13 14data.ix[-1] #\u53d6DataFrame\u4e2d\u6700\u540e\u4e00\u884c\uff0c\u8fd4\u56de\u7684\u662fSeries\u7c7b\u578b\uff0c\u8fd9\u4e2a\u4e00\u6837\uff0c\u884c\u7d22\u5f15\u4e0d\u80fd\u662f\u6570\u5b57\u65f6\u624d\u53ef\u4ee5\u4f7f\u7528Out[13]:
a 10b 11c 12d 13e 14Name: three, dtype: int32

data.tail(1) #\u8fd4\u56deDataFrame\u4e2d\u7684\u6700\u540e\u4e00\u884cdata.head(1) #\u8fd4\u56deDataFrame\u4e2d\u7684\u7b2c\u4e00\u884c123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186

\u6700\u8fd1\u5904\u7406\u6570\u636e\u65f6\u53d1\u73b0\u5f53pd.read_csv()\u6570\u636e\u65f6\u6709\u65f6\u5019\u4f1a\u6709\u8bfb\u53d6\u5230\u672a\u547d\u540d\u7684\u5217\uff0c\u4e14\u8be5\u5217\u4e5f\u7528\u4e0d\u5230\uff0c\u4e00\u822c\u662f\u7d22\u5f15\u5217\u88ab\u6362\u6389\u540e\u5bfc\u81f4\u7684\uff0c\u6709\u5f3a\u8feb\u75c7\u7684\u770b\u7740\u96be\u53d7\uff0c\u8fd9\u65f6\u5019dataframe.drop([columns,])\u662f\u6ca1\u6cd5\u5904\u7406\u7684\uff0c\u600e\u4e48\u529e\u5462\uff0c
\u6700\u7b28\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u7ed9\u5217\u7d22\u5f15\u91cd\u547d\u540d\uff1a
data6

Unnamed: 0 high symbol timedate 2016-11-01 0 3317.4 IF1611 18:10:44.82016-11-01 1 3317.4 IF1611 06:01:04.52016-11-01 2 3317.4 IF1611 07:46:25.52016-11-01 3 3318.4 IF1611 09:30:04.02016-11-01 4 3321.8 IF1611 09:31:04.0data6.columns = list('abcd')

data6 a b c ddate 2016-11-01 0 3317.4 IF1611 18:10:44.82016-11-01 1 3317.4 IF1611 06:01:04.52016-11-01 2 3317.4 IF1611 07:46:25.52016-11-01 3 3318.4 IF1611 09:30:04.02016-11-01 4 3321.8 IF1611 09:31:04.012345678910111213141516171819202122

\u91cd\u65b0\u547d\u540d\u540e\u5c31\u53ef\u4ee5\u7528dataframe.drop([columns])\u6765\u5220\u9664\u4e86\uff0c\u5f53\u7136\u4e0d\u7528\u6211\u8fd9\u6837\u5168\u90e8\u7ed9\u5217\u540d\u66ff\u6362\u6389\u4e86\uff0c\u53ef\u4ee5\u53ea\u662f\u6539\u53d8\u672a\u547d\u540d\u7684\u90a3\u4e2a\u5217\uff0c\u7136\u540e\u5220\u9664\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u7528\u8d77\u6765\u603b\u662f\u89c9\u5f97\u6709\u70b9low\uff0c\u6709\u6ca1\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u5462\uff0c\u6709\uff0c\u53ef\u4ee5\u4e0d\u53bb\u5220\u9664\uff0c\u76f4\u63a5\uff1a
data7 = data6.ix[:,1:]1

\u8fd9\u6837\u65e2\u4e0d\u6539\u53d8\u539f\u6709\u6570\u636e\uff0c\u4e5f\u8fbe\u5230\u4e86\u5220\u9664\u795e\u70e6\u5217\uff0c\u5f53\u7136\u6211\u8fd9\u91cc\u65f6\u7b2c0\u5217\u5220\u9664\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9009\u62e9\u6240\u5728\u7684\u5217\u5220\u9664\u4e4b\uff0c\u81f3\u4e8e\u8fd9\u4e2a\u539f\u7406\uff0c\u53ef\u4ee5\u770b\u4e0b\u524d\u9762\u7684\u5bf9\u5217\u7684\u64cd\u4f5c\u3002

\u76f4\u63a5\u5728\u53c2\u6570\u4e00\u680f\u8bbe\u7f6e\u4e00\u4e0b\u5373\u53ef\uff1a
df\uff1dpd.read_csv('text.csv', dtype={'\u7f16\u53f7':str}
 
\u8fd9\u6837\uff0c\u628a\u4f60\u8981\u8f6c\u6362\u7684\u5217\u7684\u540d\u5b57\u8bbe\u5b9a\u597d\uff0c \u201c\u7f16\u53f7\u201d\u5217\u4e2d\u7684\u6570\u636e\u8bfb\u53d6\u4e3astr
 
\u8fd9\u6837\uff0c\u8bfb\u53d6\u5230\u7684\u6570\u636e\u5c31\u662f\u6309\u7167\u6211\u4eec\u7684\u8981\u6c42\u7684\u4e86\u3002

建议参考一下dataframe文档,里面有相应的方法,不需要使用for循环遍历,for循环遍历会拖慢程序。对于dataframe中数据检索可以使用下面的方法。

【全部】df.values

【name列的数据】df['name'].values

【loc检索A列】df.loc['A']

【iloc进行行检索】df.iloc[0]

【直接使用名字进行列检索,但不适合行检索】df['name']

第一步:准备一些数据

根据你的描述生成的一堆数据

定义一些变量

TOPLST = ['A', 'B', 'C']   # top10
PATH = 'test.xls'            # 我生成的Excel测试文件位置

第二步:根据描述,你需要对比两个序列之间是否有交集?

这里简单定义一个函数(当然也可以不这样做):

def checkLst(lst1, lst2):
# 如果两个列表中有相同值,则返回True
for item in lst1:
if item in lst2:
return True
return False

第三步:实现我们的需求:这里主要还是使用索引,掌握dataframe的函数基础上,如何使用这些接口函数很重要。这里简单几行实现数据清洗功能。

def clearData(path, toplst):
df = pandas.read_excel(path)
# 找到需要判断的mesh列
mesh = df['mesh']
# 清除队列
clearilst = []
# 遍历每一行数据mesh.index是RangeIndex实例
for index in mesh.index:
# 判断top10和mesh列中是否有交集,如果没有交集则将索引加入删除队列
if not checkLst(toplst, mesh.iloc[index].split(';')):
# 将没有交集的行索引添加至清洗列表
clearilst.append(index)
# 清洗结果赋值
result = df.drop(index=clearilst, axis=0)
return result

完整的代码截图如下:

程序源代码

我们来看下运行效果:

运行效果展示

完美运行,不用操心索引+1的问题,也不用再创建一个DataFrame实例!

希望能够采纳!



用一个字符截取函数size就可以只读取其中的数字。

几乎所有的dataframe的操作都用不上for循环。但感觉你的描述不清晰,不明白你到底要做什么

筛选出mesh列中含有ls1元素的那行数据,dataframe其他不属于的行就舍去。我想的是创建一个空的dataframe,如果循环中的if条件满足就向这个空dataframe写入对应的行数据。求求大佬们指点。!

fetchall这个返回的是多个元组,你找到通过列表名称,获取信息了?不通过下标获取

  • pandas鈥斺Datafram鐨勫熀鏈搷浣滄柟娉
    绛旓細銆1銆Python鍦―ataframe涓柊娣诲姞涓鍒 https://blog.csdn.net/zx1245773445/article/details/99445332 銆2銆戞彃鍏:pandas-DataFrame鍒楃Щ鍔 https://blog.csdn.net/sinat_41701878/article/details/80945861 宸茶禐杩 宸茶俯杩< 浣犲杩欎釜鍥炵瓟鐨勮瘎浠锋槸? 璇勮 鏀惰捣 涓轰綘鎺ㄨ崘:鐗瑰埆鎺ㄨ崘 娉虫睜灏跨礌瓒呮爣,鏈変粈涔堝澶? 鏈夊摢浜涙彮...
  • python 鏌ョ湅dataframe姣忓垪鏈夊灏戜釜涓嶅悓鍏冪礌
    绛旓細print item + str( say.count(item)) #list.count(item) 杈撳嚭item鍦╨ist涓嚭鐜扮殑娆℃暟 鏂规硶浜岋細counts = { } #瀛楀吀for x in time_zones: #time_zones 涓哄垪琛╥f x in counts:counts[x] += 1else:counts[x] = 1print counts 鏂规硶涓夛細锛圫eries涓Datafram鐢ㄦ硶鐩稿悓锛塱mport numpy...
  • python涓璬atafram鎬庝箞for閬嶅巻鏁版嵁
    绛旓細鏈甯哥敤鐨勶紝閫氳繃for in閬嶅巻鏁扮粍 colours = ["red","green","blue"]for colour in colours:print colour red green blue
  • 鍏充簬灏忔尝杞‖闃堝兼姌涓硶鍘昏簛鐨python缂栫▼,浠涔堢粨鏋滈兘娌″嚭?璇锋眰甯姪_鐧 ...
    绛旓細Sum = (1.0 / float(N)) * Sum锛屾垜鐚滆繖涓綘搴旇鏄湪閭d釜寰幆閲岄潰鐨勫惂锛屾劅瑙変笂鏄兂鍋氫釜鍧囧硷紝涔嬪悗缁欎粬浠浉鍔狅紵浣犺繖涓啓鐨勬槸鐩告垚锛岃鏄偅涓狽寰堝ぇ鐨勮瘽鍑烘潵缁撴灉灏变細鍙樻垚(1/N)^i寰堝皬浣嗘槸sum涔熸槸i娆℃柟銆--- 娌$湅杩囧叕寮忔劅瑙変篃鏈夊彲鑳芥槸瀵圭殑锛屼箣鍚庢垜鐪嬩簡浠ヤ笅浣犻棶鐨勯棶棰橈紝璇 鐨勬槸浠涔堢粨鏋滀篃娌...
  • python 鏌ョ湅dataframe姣忓垪鏈夊灏戜釜涓嶅悓鍏冪礌
    绛旓細鏂规硶涓夛細锛圫eries涓Datafram鐢ㄦ硶鐩稿悓锛塱mport numpy as np import pandas as pd from pandas import DataFrame from pandas import Series ss = Series(['Tokyo', 'Nagoya', 'Nagoya', 'Osaka', 'Tokyo', 'Tokyo'])ss.value_counts() #value_counts 鐩存帴鐢ㄦ潵璁$畻series閲岄潰鐩稿悓鏁版嵁鍑虹幇鐨勯鐜 ...
  • 扩展阅读:photoshop永久免费版 ... c++和python先学哪个 ... photoshop在线网页版 ... python在线网站 ... javascript免费网站 ... python初学编程必背 ... python手机版下载安装 ... 最好的javascript视频 ... python编程入门自学 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网