Home > Blockchain >  I have no idea that creates a data frame by specifying an index number
I have no idea that creates a data frame by specifying an index number

Time:10-08

Here is the data frame(df), I printed out only what I wanted. I threw the rest away to see only one of the four columns. The 'df_01' is DateFrame that is composed of shape((16598,9))
I wanted to create two types of data frames with 'K' or 'M' in "EU_Sales" and a data frame consisting only of numbers.

Variable of 'x' is the index number of the data related to 'K' and '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.

CodePudding user response:

It seems like you forgot = on your code. Try it with df2 = df2.drop(index=x).

For example,

Test code

# 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)

Test result

len(x)
Out[1]: 200

len(df)
Out[2]: 16598

len(df2)
Out[3]: 16398
  • Related