[英]Append row entries of specific column to empty dataframe based on multiple conditions
我有一個名為 dictoftickersdf 的數據框字典,看起來像這樣。
我將使用字典中的 for 循環遍歷每一幀。 所以假設每一個都被稱為tickersdf。
Country Type Ticker
1 US Pub AAPL
2 US Priv etc
3 GER Pub etc
4 HK Pub etc
和
Country Type Ticker
1 US Pub GE
2 US Priv etc
3 GER Pub etc
4 HK Pub etc
5 US Pub MSFT
etc..
我有一個空的 dataframe, df = pd.DataFrame()
我正在對具有不同公司的各種tickersdf 運行for 循環。
我只想 append 基於某些條件(類型 == Pub 和 Country == US)出現在列代碼中的條目。
所以我希望最終的 df 看起來像這樣
AAPL
GE
MSFT
...
到目前為止,我有這個,
df = pd.DataFrame()
for subdir, dirs, files in os.walk(r"/Users/xxx/Documents/"):
for file in files:
filepath = os.path.join(subdir, file)
print(filepath)
dictoftickersdf = pd.read_excel(filepath,sheet_name=None) #multiple sheets per file
for key, tickersdf in dictoftickersdf.items():
df = df.append(tickersdf.loc[(tickersdf['Country']=='US') & (tickersdf['Type']=='Pub'),'Ticker'])
但是 dataframe df 出現空,我做錯了什么?
更新:
我在最后添加了一個分配命令,它不再為空,但它仍然無法正常工作。 現在df看起來像這樣
1 1 5 ...
Ticker AAPL NaN NaN ...
Ticker NaN GE MSFT ...
Ticker ....................
在您的 dataframe 中,看起來“Public”被縮短為“Pub”。 嘗試縮短該部分以查看是否可以修復它。
df = pd.Series()
for subdir, dirs, files in os.walk(r"/Users/xxx/Documents/"):
for file in files:
filepath = os.path.join(subdir, file)
print(filepath)
dictoftickersdf = pd.read_excel(filepath,sheet_name=None) #multiple sheets per file
for key, tickersdf in dictoftickersdf.items():
df = df.append(tickersdf.loc[(tickersdf['Country']=='US') & (tickersdf['Type']=='Pub'),'Ticker'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.