这是数据帧(df),我只打印出我想要的。我把剩下的都扔了,只看了四根柱子中的一根。'df_01'是由形状((16598,9))组成的DateFrame
我想创建两种类型的数据帧'K'或'M'在' eu_sales ';和一个仅由数字组成的数据帧。
变量'x'为'K'和'M'相关数据的索引号。
df_01 = df.drop(['NA_Sales','JP_Sales', 'Other_Sales'],axis =1)
df2= df_01[df_01['EU_Sales'].str.contains('K|M')]
df2.index.tolist()
x=[11,37,129,139,177,218,461,468,503,973,997,1046,1095,1264,1376,1474,1516,1556,1586,1630,1872,1896,2014,2185,2201,2270,2392,2418,2473,2484,2496,2553,
2660,2731,2907,2944,2949,3132,3330,3346,3380,3412,3476,3546,3846,4027,4050,4082,4128,4188,4232,4538,4568,4600,4688,4710,4750,4825,4899,4998,5025,5232,
5321,5500,5532,5711,5747,5861,6113,6220,6429,6459,6476,6499,6535,6645,6693,6720,6736,6780,6812,6817,6900,6907,7066,7173,7185,7320,7506,7522,7555,7654,
7688,7787,7837,7866,7917,7954,7994,8011,8069,8075,8149,8243,8245,8304,8393,8422,8511,8828,8917,9099,9120,9181,9210,9305,9400,9498,9517,9598,9661,9746,
9755,9831,9860,9929,9947,10156,10191,10275,10461,10499,10668,10844,10892,10903,10960,11078,11172,11319,11711,11809,11902,11939,11942,12420,12474,12516,
12519,12537,12584,12742,12750,13051,13067,13245,13285,13456,13531,13746,13792,13831,13889,13937,13950,14027,14085,14282,14319,14362,14394,14416,14445,14665,
14801,14822,15001,15131,15157,15212,15315,15373,15532,15707,15713,15757,15758,15844,16116,16119,16129,16137,16158,16166,16269,16348,16375,16385,16526,16572]
df2.drop(index=x)
#output :Nothing came out.
似乎您在代码中忘记了=
。试试df2 = df2.drop(index=x)
例如,
<测试代码/strong>
# create dataframe shape of (16598,9)
df = pd.DataFrame(np.random.rand(16598,9))
# your index list x
x=[11,37,129,139,177,218,461,468,503,973,997,1046,1095,1264,1376,1474,1516,1556,1586,1630,1872,1896,2014,2185,2201,2270,2392,2418,2473,2484,2496,2553,
2660,2731,2907,2944,2949,3132,3330,3346,3380,3412,3476,3546,3846,4027,4050,4082,4128,4188,4232,4538,4568,4600,4688,4710,4750,4825,4899,4998,5025,5232,
5321,5500,5532,5711,5747,5861,6113,6220,6429,6459,6476,6499,6535,6645,6693,6720,6736,6780,6812,6817,6900,6907,7066,7173,7185,7320,7506,7522,7555,7654,
7688,7787,7837,7866,7917,7954,7994,8011,8069,8075,8149,8243,8245,8304,8393,8422,8511,8828,8917,9099,9120,9181,9210,9305,9400,9498,9517,9598,9661,9746,
9755,9831,9860,9929,9947,10156,10191,10275,10461,10499,10668,10844,10892,10903,10960,11078,11172,11319,11711,11809,11902,11939,11942,12420,12474,12516,
12519,12537,12584,12742,12750,13051,13067,13245,13285,13456,13531,13746,13792,13831,13889,13937,13950,14027,14085,14282,14319,14362,14394,14416,14445,14665,
14801,14822,15001,15131,15157,15212,15315,15373,15532,15707,15713,15757,15758,15844,16116,16119,16129,16137,16158,16166,16269,16348,16375,16385,16526,16572]
df2 = df.copy()
#use pd.drop() to drop index x
df2 = df2.drop(x)
测试结果
len(x)
Out[1]: 200
len(df)
Out[2]: 16598
len(df2)
Out[3]: 16398