![](/img/trans.png)
[英]How to copy input from entry field and copy to clipboard with Tkninter and Python?
[英]Python: If any entry that is empty copy the entry above it
我有以下字符串變量
data='Industry \t& Company \t\t\t \t\t & Variable Name \n Oil \t& Mobil \t\t\t \t\t & MOBIL \n \t& Texaco \t\t\t \t\t & TEXACO \n Computers \t& IBM \t\t\t \t\t & IBM \n \t \t& Digital Equipment Co. \t\t & DEC \t\t \n \t \t& Data General \t\t\t\t\t & DATGEN \n Electricity & Consolidated Edison \t\t & CONED \n \t & Public Service of New Hampshire & PSNH \n \t & General Public Utilities \t\t & GPU \n Forestry & Weyerhauser \t\t\t\t\t & WEYER \n \t & Boise \t\t\t\t\t\t & BOISE \n Electronics & Motorola \t\t\t\t\t\t & MOTOR \n \t & Tandy \t\t\t\t\t\t & TANDY \n Airlines & Pan American \t\t\t\t\t & PANAM \n \t & Delta \t\t\t\t\t\t & DELTA \n Banks & Continental Illinois \t\t\t & CONTIL \n \t & Citicorp\t\t\t\t\t\t & CITCRP \n Food & Gerber \t\t\t\t\t\t & GERBER \n \t & General Mills \t\t\t\t & GENMIL \n Chemicals & Dow \t\t\t\t\t\t & DOW \n \t & Dupont \t\t\t\t\t\t & DUPONT \n \t & Conoco \t\t\t\t\t\t & CONOCO '
我能夠使用以下代碼將其轉換為熊貓表(如果您有更簡單的方法會很好)
lines = data.split("\n")
array = np.zeros(shape=(len(lines),3))
array=array.astype('str')
for i1 in range(len(lines)):
set1=lines[i1].split('&')
for i, v in enumerate(set1):
set1[i]=v.replace('\t', '').replace(' ', '')
for i2 in range(3):
array[i1,i2]=set1[i2]
df=pd.DataFrame(array[1:],columns=array[0])
所以現在我的df看起來如下
有沒有辦法更換空電池,如 0 油中的一個和 1、1 到計算機和 2,2 到電力。 這樣空單元格就會復制其上方的單元格。
非常感謝你提前
利用 -
df['Industry'] = df['Industry'].replace('', np.nan).ffill()
Output
0 Oil
1 Oil
2 Computers
3 Computers
4 Computers
5 Electricity
6 Electricity
7 Electricity
8 Forestry
9 Forestry
10 Electronics
11 Electronics
12 Airlines
13 Airlines
14 Banks
15 Banks
16 Food
17 Food
18 Chemicals
19 Chemicals
20 Chemicals
Name: Industry, dtype: object
使用str.get_dummies()
參考這個, https://www.geeksforgeeks.org/python-pandas-series-str-get_dummies/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.