我知道如何使用Python从数据框中删除列.但是对于我的问题,数据集很大,我想要删除的列被组合在一起,或者基本上是在列标题轴上单独展开.是否有一种更短的方法来切割或删除所有列的代码行数较少,而不是像我所做的那样将其写出来.我在这里的方式是有效的,但我想要一个更加总结的方式.
flight_data_copy_final是应该存储它的变量.
提前致谢
这是我的代码:
from IPython.display import display
flight_data_copy_version1 = flight_data_copy.drop(flight_data_copy.ix[:,"Year": "FlightDate"].columns, axis=1)
flight_data_copy_version2 = flight_data_copy_version1.drop("TailNum", axis=1)
flight_data_copy_version3 = flight_data_copy_version2.drop("OriginStateFips", axis=1)
flight_data_copy_version4 = flight_data_copy_version3.drop("DestStateFips", axis=1)
flight_data_copy_version5 = flight_data_copy_version4.drop("Diverted", axis=1)
flight_data_copy_version6 = flight_data_copy_version5.drop("Flights", axis=1)
flight_data_copy_final = flight_data_copy.drop(flight_data_copy_version6.ix[:,"FirstDepTime":].columns, axis=1)
print (display (flight_data_copy_final))
Run Code Online (Sandbox Code Playgroud) 我想使用多个列使用多个条件从数据帧中过滤掉数据.我尝试这样做:
arrival_delayed_weather = [[[flight_data_finalcopy["ArrDelay"] > 0]] & [[flight_data_finalcopy["WeatherDelay"]>0]]]
arrival_delayed_weather_filter = arrival_delayed_weather[["UniqueCarrier", "AirlineID"]]
print arrival_delayed_weather_filter
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误消息:
TypeError:&''list'和'list'不支持的操作数类型
我该如何解决这个问题?
提前致谢
我正在尝试创建一个使用df.iterrows()和的函数Series.nlargest.我想遍历每一行并找到最大的数字,然后将其标记为a 1.这是数据框:
A B C
9 6 5
3 7 2
Run Code Online (Sandbox Code Playgroud)
这是我希望的输出:
A B C
1 0 0
0 1 0
Run Code Online (Sandbox Code Playgroud)
这是我想在这里使用的功能:
def get_top_n(df, top_n):
"""
Parameters
----------
df : DataFrame
top_n : int
The top number to get
Returns
-------
top_numbers : DataFrame
Returns the top number marked with a 1
"""
# Implement Function
for row in df.iterrows():
top_numbers = row.nlargest(top_n).sum()
return top_numbers
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:AttributeError:'tuple'对象没有属性'nlargest'
如何以更整洁的方式重新编写我的功能并实际工作,将不胜感激!提前致谢
您好,我已经训练并测试了数据。我正在尝试使用sklearn的特征相关性Seelct K Best来选择相关特征并在之后绘制条形图。但是我收到这个错误:
ValueError: could not convert string to float: B
Run Code Online (Sandbox Code Playgroud)
但我开始认为我的数据集中确实有一列看起来像这样,这可能是问题所在:
CancellationCode:
A
B
C
D
Run Code Online (Sandbox Code Playgroud)
如果此列导致问题,我该如何解决此错误 这是我的代码如下:
import numpy as np
from sklearn.feature_selection import SelectKBest, f_classif
import matplotlib.pyplot as plt
selector = SelectKBest(f_classif, k=13)
selector.fit(X_train, y_train)
scores_select = selector.pvalues_
print scores_select
# Plotting the bar Graph to visually see the weight of each feature
plt.bar(range(len(scores_select)), scores_select, align='center')
plt.xticks(range(len(features_columns)), features_columns, rotation='vertical')
plt.show()
Run Code Online (Sandbox Code Playgroud) 你好我有一个字典,看起来像这样:
dictionary = {'John': {'car':12, 'house':10, 'boat':3},
'Mike': {'car':5, 'house':4, 'boat':6}}
Run Code Online (Sandbox Code Playgroud)
我希望获得访问权并提取子字典中的密钥,并将它们分配给这样的变量:
cars_total = dictionary['car']
house_total = dictionary['house']
boat_total = dictionary['boat']
Run Code Online (Sandbox Code Playgroud)
现在,当我运行上面的变量时,我得到一个'Key Error'.这是可以理解的,因为我需要首先访问外部字典.如果有人帮助了解如何访问密钥和子字典中的值,我会很感激,因为那些是我想要使用的值.
另外我想创建一个新密钥,这可能不对,但在这些方面的东西:
car = dictionary['car']
house = dictionary['house']
boat = dictionary['boat']
dictionary['total_assets'] = car + house + boat
Run Code Online (Sandbox Code Playgroud)
我希望能够访问字典中的所有键并创建新密钥.诸如"John"和"Mike"之类的外键都应该包含最后创建的键.我知道这会引发错误,但它会让你知道我想要实现的目标.感谢您的帮助