2
我有下面的頭一個數據幀:爆炸一行到多行大熊貓數據幀
id, type1, ..., type10, location1, ..., location10
,我想將它轉換爲:
id, type, location
我能夠做到這一點使用嵌入for循環,但它很慢:
new_format_columns = ['ID', 'type', 'location']
new_format_dataframe = pd.DataFrame(columns=new_format_columns)
print(data.head())
new_index = 0
for index, row in data.iterrows():
ID = row["ID"]
for i in range(1,11):
if row["type"+str(i)] == np.nan:
continue
else:
new_row = pd.Series([ID, row["type"+str(i)], row["location"+str(i)]])
new_format_dataframe.loc[new_index] = new_row.values
new_index += 1
任何使用本地熊貓功能的改進建議?
你的數據集有多大? – MMF
@MMF現在幾GB – MedAli