我有一个样本DF:
df = pd.DataFrame(np.random.randint(0,10,size = (6,2)),columns=["A","B"])
df["col"] = ["Apple","Apple","Mango","Apple","Mango","Apple"]
df
OP:
A B col
0 3 9 Apple
1 8 8 Apple
2 7 9 Mango
3 2 4 Apple
4 4 5 Mango
5 1 6 Apple
我试图获取列"A" and "B"
中有重复值的地方col
的值。例如,列col
在索引-Apple
中的值为0,1,3,5
,而我试图在列A and B
中获取相应的值,即
{"Apple":[[3,9],[8,8],[2,4],[1,6]], "Mango": [[7,9],[4,5]]}
我有一个迭代方法,在大型Dfs上花费很长时间。
当前方法:
->在col
列中查找唯一值
unique_values = list(df["col"].unique())
->遍历此列表,并遍历DF的每一行以获取所需的OP:
op = {}
for i in range(len(unique_values)):
for index, rows in df.iterrows():
if rows ["col"] == unique_values[i]:
inner_op = []
inner_op.append(rows["A"]);inner_op.append(rows["B"])
if rows["col"] in op:
op[rows["col"]].append(inner_op)
else:
op[rows["col"]] = [inner_op]
最终操作:
{'Apple': [[3, 9], [8, 8], [2, 4], [1, 6]], 'Mango': [[7, 9], [4, 5]]}
有没有建议采用更有效的熊猫方法?
答案 0 :(得分:1)
创建由列表SendRiepilogo
填充的列,将值转换为numpy数组,然后将其转换为列表,然后由GroupBy.agg
与Series.to_dict
聚合public static async void SendRiepilogo(string piva, int idOrdine)
{
var order = GetOrdine(piva, idOrdine);
if (order == null)
{
return;
}
try
{
var negozio = getNegozio(order.idNegozio);
var from = new MailAddress("ordini@visualorder.it", "VisualOrder");
var to = new MailAddress(order.cliente.FirstOrDefault().email);
using MemoryStream ms = new MemoryStream();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("vo/" + idOrdine, QRCodeGenerator.ECCLevel.Q);
Base64QRCode qrCode = new Base64QRCode(qrCodeData);
byte[] byteQr = Convert.FromBase64String(qrCode.GetGraphic(20));
MemoryStream streamQr = new MemoryStream(byteQr);
var qrImage = new LinkedResource(streamQr, MediaTypeNames.Image.Jpeg)
{
ContentId = "qrImage"
};
string nome = order.cliente.FirstOrDefault().nome;
var orderEmail = new { idOrdine, order, nome, negozio };
byte[] byteLogo = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(negozio.logo));
MemoryStream streamLogo = new MemoryStream(byteLogo);
var logoImage = new LinkedResource(streamLogo, MediaTypeNames.Image.Jpeg)
{
ContentId = "logoImage"
};
string template = File.ReadAllText("Views/Emails/EmailRiepilogo.cshtml");
var htmlBody = Engine.Razor.RunCompile(template, "riepilogo", null, orderEmail);
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(qrImage);
alternateView.LinkedResources.Add(logoImage);
var message = new MailMessage(from, to)
{
Subject = "Riepilogo ordine",
Body = htmlBody
};
message.IsBodyHtml = true;
message.AlternateViews.Add(alternateView);
using var smtp = new SmtpClient("smtps.aruba.it", 587)
{
EnableSsl = true,
Credentials = new NetworkCredential("XXX", "XXX")
};
await smtp.SendMailAsync(message); // sending email to user
await smtp.SendMailAsync(MessageNegozio(order, idOrdine, negozio)); // sending email to shop
}
catch (Exception e)
{
return;
}
ConfirmEmail(piva, idOrdine); // setting "EMAIL SENT" flag in DB to true
return;
}
:
C