[英]Is there a way to add a new column to a pandas dataframe, appending each unique value of the new column to every existing row of the dataframe?
給出一個熊貓數據幀:
fruit_prices = [('apple', 5.99),
('orange', 4.99),
('pear', 6.99)]
labels = ['fruit', 'price']
fruit_prices = pd.DataFrame.from_records(datasets, columns=labels)
fruit_prices
fruit price
apple 5.99
orange 4.99
apple 6.99
我想添加一個新列,例如,只包含兩個值,但是這樣的方式是每個這些唯一值都出現在原始數據幀中的每個現有行中。
day = ['wednesday', 'wednesday', 'thursday']
預期產量:
fruit price day
apple 5.99 wednesday
apple 5.99 thursday
orange 4.99 wednesday
orange 4.99 thursday
apple 6.99 wednesday
apple 6.99 thursday
我想也許我可以在從新列/系列中獲取唯一值之后使用itertools:
from itertools import cycle
dates = cycle(['wednesday','thursday'])
但我不知道如何將其分配回數據幀(以允許重復現有行的方式)或者這甚至是一種可行的方法。 我還考慮過從該系列創建一個單列數據框並合並它,但這看起來很迂回,我也不知道如何去做。
我相信你需要cross join
:
day = ['wednesday', 'thursday']
df = fruit_prices.assign(A=1).merge(pd.DataFrame({'day':day,'A':1}), on='A', how='outer')
print (df)
fruit price A day
0 apple 5.99 1 wednesday
1 apple 5.99 1 thursday
2 orange 4.99 1 wednesday
3 orange 4.99 1 thursday
4 pear 6.99 1 wednesday
5 pear 6.99 1 thursday
使用itertools.cycle
:
day = ['wednesday', 'wednesday', 'thursday']
#list(set(day)
#['wednesday', 'thursday']
from itertools import cycle, islice
df_new=pd.concat([df,df[::-1]],ignore_index=True)
df_new['day']=list(islice(cycle(list(set(day) )), len(df_new)))
print(df_new)
fruit price day
0 apple 5.99 wednesday
1 orange 4.99 thursday
2 apple 6.99 wednesday
3 apple 6.99 thursday
4 orange 4.99 wednesday
5 apple 5.99 thursday
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.