【问题标题】:I have no idea that creates a data frame by specifying an index number [closed]我不知道通过指定索引号来创建数据框[关闭]
【发布时间】:2023-03-13 21:50:01
【问题描述】:

这是数据框(df),我只打印了我想要的。 我把其余的扔掉,只看到四列中的一列。 'df_01' 是由 shape((16598,9))
组成的 DateFrame 我想在“EU_Sales”中创建两种类型为“K”或“M”的数据框,以及一个仅包含数字的数据框。

“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.

【问题讨论】:

  • 请用一个简单的例子和​​预期的结果来说明您的问题。 How to Askminimal reproducible example
  • 你能提供预期的输出吗?
  • @Julien 哦,对不起,我在写这个问题时犯了很多错误。下次我会小心的:)

标签: python pandas dataframe pandas-groupby


【解决方案1】:

您似乎忘记了代码中的=。试试df2 = df2.drop(index=x)

例如,

测试代码

# 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

【讨论】:

  • 哦!我很抱歉迟到了回复!因为我不擅长说英语。哦.. 'df_01' 是由 shape((16598,9)) 'x' 组成的 DateFrame 我指定了要与变量'x'分开提取的索引号。 @康
  • 试试df2 = df2.drop(index=x)。好像错过了=
  • 哦!!!!!!baaaaamm!!!!我真的很高兴能解决这个问题!!谢谢 !!!!! @康
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 2020-08-06
  • 2015-05-15
  • 1970-01-01
  • 2013-01-21
  • 2014-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多