我想将数据框中列的第一个n
元素替换为我保存的另一个pd.series。举个例子,
category price store testscore
0 Cleaning 11.42 Walmart NaN
1 Cleaning 23.50 Dia NaN
2 Entertainment 19.99 Walmart NaN
3 Entertainment 15.95 Fnac NaN
4 Tech 55.75 Dia NaN
5 Tech 111.55 Walmart NaN
在这里,我想用一组新的字符串替换testscore中的前三个NaN。
想象一下,我有一个变量:
cats = pd.Series(df['category'][0:2])
我可以将它放在testscore列中......
category price store testscore
0 Cleaning 11.42 Walmart Cleaning
1 Cleaning 23.50 Dia Cleaning
2 Entertainment 19.99 Walmart Entertainment
3 Entertainment 15.95 Fnac NaN
4 Tech 55.75 Dia NaN
5 Tech 111.55 Walmart NaN
但每当我尝试这样做时,它都不会工作。
创建此假数据集的代码:
import pandas as pd
import numpy as np
df = pd.DataFrame({'category': ['Cleaning', 'Cleaning', 'Entertainment', 'Entertainment', 'Tech', 'Tech'],
'store': ['Walmart', 'Dia', 'Walmart', 'Fnac', 'Dia','Walmart'],
'price':[11.42, 23.50, 19.99, 15.95, 55.75, 111.55],
'testscore': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]})
print(df)
df2 = pd.DataFrame({'category': ['Cleaning', 'Cleaning', 'Entertainment', 'Entertainment', 'Tech', 'Tech'],
'store': ['Walmart', 'Dia', 'Walmart', 'Fnac', 'Dia','Walmart'],
'price':[11.42, 23.50, 19.99, 15.95, 55.75, 111.55],
'testscore': ['Cleaning', 'Cleaning', 'Entertainment', np.nan, np.nan, np.nan]})
print(df2)
答案 0 :(得分:2)
只需使用df.loc:
taRegisterTool('ul', {
iconclass: 'fa fa-list-ul',
action: function(){
return this.$editor().wrapSelection("insertUnorderedList", null);
},
activeState: function(){ return document.queryCommandState('insertUnorderedList'); }
});
你得到:
import pandas as pd
import numpy as np
df = pd.DataFrame({'category': ['Cleaning', 'Cleaning', 'Entertainment', 'Entertainment', 'Tech', 'Tech'],
'store': ['Walmart', 'Dia', 'Walmart', 'Fnac', 'Dia','Walmart'],
'price':[11.42, 23.50, 19.99, 15.95, 55.75, 111.55],
'testscore': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]})
cats = pd.Series(df['category'][:3]) # 3 elements
df.loc[:3,'testscore'] = cats # Assign first 3
print(df)
答案 1 :(得分:2)
将find . ... -path ./logs ...
与参数fillna
:
limit
输出:
df['testscore'] = df.testscore.fillna(df.category, limit=3)
df