假设我有一个列表的DataFrame,
my_df = pd.DataFrame({'my_list':[[45,12,23],[20,46,78],[45,30,45]]})
产生以下结果:
my_list
0 [45, 12, 23]
1 [20, 46, 78]
2 [45, 30, 45]
如何为每行my_list
添加一个元素,假设为99?
预期结果:
my_list
0 [45, 12, 23, 99]
1 [20, 46, 78, 99]
2 [45, 30, 45, 99]
答案 0 :(得分:5)
In [90]: my_df['my_list'] += [99]
In [91]: my_df
Out[91]:
my_list
0 [45, 12, 23, 99]
1 [20, 46, 78, 99]
2 [45, 30, 45, 99]
答案 1 :(得分:3)
听起来非常无聊但只是直接迭代这些值 - 这样你就可以调用namespace ConsoleApp1
{
class Program
{
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task<string> GetProductAsync(string path = "https://www.fourmilab.ch/cgi-bin/Hotbits.api?nbytes=8&fmt=xml&npass=1&lpass=8&pwtype=3&apikey=HB18CsHhr5Muzoee1KAu4QY5xUe")
{
string product = string.Empty;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<string>();
}
return product;
}
static async Task RunAsync()
{
string str = await GetProductAsync();
XmlDocument xml = new XmlDocument();
xml.Load(str);
Console.WriteLine(xml.InnerXml);
XmlNodeList list = xml.GetElementsByTagName("random-data");
string[] strs = list[0].InnerXml.Split(' ');
foreach (object e in strs)
{
Console.WriteLine(e);
}
}
并避免append
发生任何重新绑定,从而大大加快了速度。
+=
<强>演示强>
for val in my_df.my_list:
val.append(99)
当然,如果速度(或者即使不是速度)对您很重要,您应该质疑确实是否需要在DataFrame中包含列表。考虑使用NumPy数组,直到您需要Pandas实用程序并执行类似
的操作>>> import timeit
>>> setup = '''
import pandas as pd; import numpy as np
df = pd.DataFrame({'my_list': np.random.randint(0, 100, (500, 500)).tolist()})
'''
>>> min(timeit.Timer('for val in df.my_list: val.append(90)',
setup=setup).repeat(10, 1000))
0.05669815401779488
>>> min(timeit.Timer('df.my_list += [90]',
setup=setup).repeat(10, 1000))
2.7741127769695595
或至少将您在DataFrame中的列表拆分为单独的列并指定新列。