如何將Excel檔案與python結合?(columnAB columnC) 我用pandas合并Excel檔案,但是不行。
!pip install xlrd
!pip install xlwt
!pip install openpyxl
import xlrd
import xlwt
import openpyxl
import pandas as pd
df = pd.DataFrame()
for f in ['MOMO摩天-專品商品名稱購買人數.xls', "MOMO摩天-專品價格.xls"]:
data = pd.read_excel(f, 'Sheet1')
data.index = [os.path.basename(f)]
df = df.append(data)
df.to_excel('Combine.xls')
uj5u.com熱心網友回復:
您可以將 Excel 檔案轉換為兩個不同的 Dataframe,然后使用此函式將兩個 Dataframe 合并為一個:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html
前任:
> df1 = pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'],
'value': [1, 2, 3, 5]})
> df2 = pd.DataFrame({'rkey': ['foo', 'bar', 'baz', 'foo'],
'value': [5, 6, 7, 8]})
> df1
lkey value
0 foo 1
1 bar 2
2 baz 3
3 foo 5
> df2
rkey value
0 foo 5
1 bar 6
2 baz 7
3 foo 8
> df1.merge(df2, left_on='lkey', right_on='rkey')
lkey value_x rkey value_y
0 foo 1 foo 5
1 foo 1 foo 8
2 foo 5 foo 5
3 foo 5 foo 8
4 bar 2 bar 6
5 baz 3 baz 7
uj5u.com熱心網友回復:
您可以使用Pandas 'read_excel'將Excel檔案匯入Python。
import pandas as pd
#Replace - Path where the Excel file is stored\File name.xlsx
#Replace - your Excel sheet name
df = pd.read_excel (r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name')
print (df)
在 Pandas 中合并兩列。
您可以使用以下語法將 Pandas DataFrame 中的兩個文本列合并為一個:
#Replace - Column Names with applicable column names
df['new_column'] = df['column1'] df['column2']
您可以使用to_excel將 Pandas DataFrame匯出到Excel檔案
#Replace - Path where the exported excel file will be stored\File Name.xlsx
df.to_excel(r'Path where the exported excel file will be stored\File Name.xlsx', index = False)
uj5u.com熱心網友回復:
將熊貓匯入為 pd
df1 = pd.read_excel(name1) #if doesn't work, change file format to xlsx
df2 = pd.read_excel(name2)
現在使用合并或加入。例如:
df = df1.join(df2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373290.html
