[英]How to add data entered by the user to a pandas data frame column?
我有以下數據集:
import pandas as pd
data = {'type': ['train', 'train', 'train', 'pool', 'pool',
'pool', 'pool', 'pool'], 'index': [0,1,2,3,4,5,6,7], 'corpus':
['a','b','c', 'd', 'e', 'f', 'g', 'h'], 'labels': [[1,0,0],
[0,1,0], [1,1,0], None , None , None , None , None]}
data = pd.DataFrame(data)
data
我想要做的是向用戶顯示與列“類型”“池”相關的“語料庫”列中的數據,並為其添加一些標簽。 之后,我的程序應該能夠在數據集中插入用戶添加到顯示的每個文本的標簽。 使用下面的代碼,程序將添加用戶輸入的最后一個 label 並替換原始數據集的所有標簽。
for row, c in data.iterrows():
if c['type'] == 'pool':
a = input(f"Please enter your labels for
the below text: \n\n {c['corpus']}")
data['labels'] = a
所以,我的 output 當前 output 是:
type corpus labels
0 train a 0,0,1
1 train b 0,0,1
2 train c 0,0,1
7 pool h 0,0,1
4 pool e 0,0,1
3 pool d 0,0,1
5 pool f 0,0,1
6 pool g 0,0,1
我的目標是:
type corpus labels
0 train a [1, 0, 0]
1 train b [0, 1, 0]
2 train c [1, 1, 0]
7 pool h [1, 0, 0]
4 pool e [0, 0, 1]
3 pool d [1, 1, 1]
5 pool f [0, 1, 0]
6 pool g [0, 0, 1]
代碼有兩點需要修復:
首先,如果您將a
分配給data['labels']
您實際上是將其分配給整個列(這就是為什么您在所有行中都獲得相同的值)。
其次,分配input
的返回值將分配一個字符串,但其他行包含一個整數列表。 為了解決這個問題,我們可以使用split
來獲取元素 map int
並使用df.at
import pandas as pd
data = {
"type": ["train", "train", "train", "pool", "pool", "pool", "pool", "pool"],
"index": [0, 1, 2, 3, 4, 5, 6, 7],
"corpus": ["a", "b", "c", "d", "e", "f", "g", "h"],
"labels": [[1, 0, 0], [0, 1, 0], [1, 1, 0], None, None, None, None, None],
}
data = pd.DataFrame(data)
print(data)
for idx, row in data.iterrows():
if row["type"] == "pool":
a = input(f"Please enter your labels for the below text: \n\n {row['corpus']} ")
data.at[idx, "labels"] = list(map(int, a.split(",")))
print(data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.