使用for循环在数据框中添加值

时间:2019-06-27 04:28:24

标签: python pandas selenium dataframe

我是StackOverflow的新手

这是我第二次问类似的问题,因为第一个不清楚,并且重复。顺便说一下,我是新手,正在尝试学习网络抓取。

这是我到目前为止所做的:

我有一个字符串列表,其中第一个索引是具有下一个索引作为其值的列名。同样,第三个索引是列名,但具有不同名称的第四个索引是列值。

我想将所有这些列表放入具有列名“ i”,值为“ i_next”的数据框中

text=my_detail[0].split('\n')
#for example text=['a','2','b','3',c,'4'] <is a list not dataframe>
#some of the string in text is not require
#example the text can be ['a','2','f','b','3','c','4']

df = pd.DataFrame(columns=['a','b','c'])

for i,nexti in zip(text,text[1:]):

    if i in df.columns:
        #store df at column name i having value nexti  
'''
The expected answer is
a b c
2 3 4
'''

万一这个问题再次出现任何问题或重复,您可以发表评论,我将其删除。

感谢您的考虑,

1 个答案:

答案 0 :(得分:3)

使用DataFrame构造函数,通过索引获取值:

df = pd.DataFrame([text[1::2]], columns=text[::2])
print (df)
   a  b  c
0  2  3  4

编辑:

解决方案循环-想法是创建字典列表,并将其传递给DataFrame构造函数:

L= [['a\n2','b\n3','c\n4'], ['a\n20','b\n30','c\n40']]

final = []
for x in L:
    inner = {}
    for y in x:
        text = y.split('\n')
        for a, b in zip(text[::2],text[1::2]):
            inner[a] = b
    final.append(inner)

print (final)
[{'a': '2', 'b': '3', 'c': '4'}, {'a': '20', 'b': '30', 'c': '40'}]

df = pd.DataFrame(final)
print (df)

    a   b   c
0   2   3   4
1  20  30  40