Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Dataframe:如何从Dataframe中的现有列表创建列?_Python_Pandas_Dataframe - Fatal编程技术网

Python Dataframe:如何从Dataframe中的现有列表创建列?

Python Dataframe:如何从Dataframe中的现有列表创建列?,python,pandas,dataframe,Python,Pandas,Dataframe,所以我有一个来自csv文件的熊猫数据帧,如下所示: year,month,day,list 2017,09,01,"[('United States of America', 12345), (u'Germany', 54321), (u'Switzerland', 13524), (u'Netherlands', 24135), ... ] 2017,09,02,"[('United States of America', 6789), (u'Germany', 9876), (u'Switz

所以我有一个来自csv文件的熊猫数据帧,如下所示:

year,month,day,list
2017,09,01,"[('United States of America', 12345), (u'Germany', 54321), (u'Switzerland', 13524), (u'Netherlands', 24135), ... ]
2017,09,02,"[('United States of America', 6789), (u'Germany', 9876), (u'Switzerland', 6879), (u'Netherlands', 7968), ... ]
country = df.apply(lambda x:[x['list'][0]]).stack().reset_index(level=1, drop=True)
count  = df.apply(lambda x:[x['list'][1]]).stack().reset_index(level=1, drop=True)
df.drop('list', axis=1).join(country).join(count)
每行第4列中的国家/地区计数对数不相同。
我想展开第4列中的列表,并将数据帧转换为如下内容:

year,month,day,country,count
2017,09,01,'United States of America',12345
2017,09,01,'Germany',54321
2017,09,01,'Switzerland',13524
2017,09,01,'Netherlands',24135
...
2017,09,02,'United States of America',6789
2017,09,02,'Germany',9876
2017,09,02,'Switzerland',6879
2017,09,02,'Netherlands',7968
...
我的想法是生成两个独立的列,然后将它们连接到原始数据帧。也许是这样的:

year,month,day,list
2017,09,01,"[('United States of America', 12345), (u'Germany', 54321), (u'Switzerland', 13524), (u'Netherlands', 24135), ... ]
2017,09,02,"[('United States of America', 6789), (u'Germany', 9876), (u'Switzerland', 6879), (u'Netherlands', 7968), ... ]
country = df.apply(lambda x:[x['list'][0]]).stack().reset_index(level=1, drop=True)
count  = df.apply(lambda x:[x['list'][1]]).stack().reset_index(level=1, drop=True)
df.drop('list', axis=1).join(country).join(count)
上面的代码肯定不起作用(我只是希望它能帮助表达我的想法),我也不知道如何扩展日期列。

非常感谢您的任何帮助或建议

解决问题的最简单方法可能是迭代数据帧中包含的元组,并创建一个新元组。您可以使用两个嵌套的for循环来完成此操作

df_new = []
for i in df.itertuples():
    for l in i.list:
        df_new.append([i.year, i.month, i.day, l[0], l[1]])

df_new = pd.DataFrame(df_new, columns=['year', 'month', 'day', 'country', 'count'])
如果列表的第四个字段不是实际的列表,而是字符串(dataframe示例中的双引号让我有些怀疑),您可以使用
ast
库中的
literal\u eval
函数:

使用:

import ast
#convert strings to lists of tuples
df['list'] = df['list'].apply(ast.literal_eval)
#create reshaped df from column list
df1 =pd.DataFrame([dict(x) for x in df['list'].values.tolist()]).stack().reset_index(level=1)
df1.columns = ['country','count']
#join to original
df = df.drop('list', 1).join(df1).reset_index(drop=True)
print (df)
   year  month  day                   country  count
0  2017      9    1                   Germany  54321
1  2017      9    1               Netherlands  24135
2  2017      9    1               Switzerland  13524
3  2017      9    1  United States of America  12345
4  2017      9    2                   Germany   9876
5  2017      9    2               Netherlands   7968
6  2017      9    2               Switzerland   6879
7  2017      9    2  United States of America   6789

因此,您需要的是将包含值列表的列转换为多行。一种解决方案是创建一个新的数据帧并执行左键:

最后,使用
reset\u index()


信用证:

非常感谢!我将尝试这种方法,看看它是否有效。你是对的,第4列不是一个实际的列表,而是一个字符串,你的方法确实解决了日期问题。非常感谢。非常感谢。我试过了,这正是我需要的。顺便说一句,我发现日期有问题,问题可能是重新加入部分。如果我知道如何更正,我会更新。谢谢!我也要这样试试。