How do I wrap text in a Table cell? - tabular

I would like to wrap a text in a table. I've tried the following but the text exceeds both table width and page.
\begin{landscape}
\begin{table}
\begin{center}
\begin{tabular}{lll}
\multicolumn{3}{l}{Table 1: Dataset, Sources and Construction} \\ \hline \hline
No & Abbrev. & Construction \\ \hline
1 & EquityM & Logarithmic returns of 6 equity markets (FTSE100, NIKKEI225, SPI, DAX30, SPTSX, AllOrds for UK, Japan, Switzerland, Eurozone, Canada, Australia, respectively) \\
\hline
\end{tabular}
\end{center}
\end{table}
\end{landscape}

It works for me:
\begin{figure}[h]
\centering
\begin{tabular}{ |p{2cm}|p{2cm}|p{6cm}| }
\hline
No & Abbrev. &Construction\\
\hline
1 & EquityM & Logarithmic returns of 6 equity markets (FTSE100, NIKKEI225, SPI, DAX30, SPTSX, AllOrds for UK, Japan, Switzerland, Eurozone, Canada, Australia, respectively) \\
\hline
\end{tabular}
\caption{Dataset, Sources and Construction}
\label{fig:result}
\end{figure}

Related

Faster Way to Categorize Unique Row Combinations based on a Condition in Python Pandas?

I have a dataset that has the same layout as the one below. I want to categorize where each person went in their car for a given month.
Date
Visitor
Car Name
Location
6-2022
Michael
Chrysler
Work
6-2022
Jim
Subaru
Home
6-2022
Jim
Subaru
Work
6-2022
Michael
Chrysler
Karate
7-2022
Michael
Chrysler
Work
7-2022
Jim
Subaru
Home
7-2022
Jim
Subaru
Work
The solution would look something like this:
Date
Visitor
Car Name
Location
6-2022
Michael
Chrysler
Work & Karate
6-2022
Jim
Subaru
Work & Home
7-2022
Michael
Chrysler
Work
7-2022
Jim
Subaru
Work & Home
I HAVE a solution but it scales linearly with time as the number of rows increases. The fastest I have gotten it to run over ~32,000 rows is ~15 minutes.
You could try as follows:
Use df.groupby on Date, Visitor, Car Name, and apply join to the values of Location.
To change the last , delimiter to an ampersand, you could use Series.replace with a regex pattern.
res = df.groupby(['Date','Visitor','Car Name'],
as_index=False, sort=False)['Location'].agg(', '.join)
# changing each last `,` delimiter into an `&`
res['Location'] = res['Location'].replace(r',\s(?=[^,]*$)',' & ', regex=True)
print(res)
Date Visitor Car Name Location
0 6-2022 Michael Chrysler Work & Karate
1 6-2022 Jim Subaru Home & Work
2 7-2022 Michael Chrysler Work
3 7-2022 Jim Subaru Home & Work
If you expect duplicates per group (and want to get rid of them), you could use .agg(lambda x: ', '.join(set(x))) instead of .agg(', '.join). Mind you, this will affect the order, not sure if this is a problem. If so, there are of course also ways to get rid of duplicates in a list, while maintained order.
Try this:
df.groupby(['Date', 'Visitor', 'Car Name'], as_index=False)['Location'].agg(' & '.join)
Output:
Date Visitor Car Name Location
0 6-2022 Jim Subaru Home & Work
1 6-2022 Michael Chrysler Work & Karate
2 7-2022 Jim Subaru Home & Work
3 7-2022 Michael Chrysler Work

Why do we use "&cell_reference&" in Excel?

Lets say we have a table like this:
A
B
1
Aaron
20
2
Bob
30
What is the difference between =""&A1&" has "&B1&" cakes" and = A1 & " has " & B1 & " cakes"?
Why do we use "&cell_reference&" anyways?

Extracting Data from a line

I have some data that looks like this (This is real countries but no sensitive data) It is formatted exactly like i posted
Calgary, Alberta T2P 0B3 Canada
Thunder Bay, Ontario P7G 0A1 Canada
Toronto, Ontario M3A 1E5 Canada
Calgary, Alberta T2G 1C6 Canada
Calgary, Alberta T2N 1C2 Canada
Edmonton, Alberta T5J 2W8 Canada
New Westminster, British Columbia V3L 5T4 Canada
London, Ontario N6A 5P6 Canada
Guelph, Ontario N1G 2W1 Canada
Whitby, Ontario L1N 4Z1 Canada
Red Deer, Alberta T4R 0L4 Canada
Edmonton, Alberta T5H 0S2 Canada
West Kelowna, British Columbia V4T 3E3 Canada
I am trying to get the Canadian zip code (N6A 5P6, N1G 2W1, etc) in 1 cell (so 1 cell per line with the zipcode.
So it will look like
Original data, zip code
original data, zip code
I figured out how to do it for american zip codes, but that space is messing me up. Thank you.
I think this should work, assuming it always ends with Canada:
=MID(A1,SEARCH("Canada",A1)-8,8)
You could split the content on spaces and just drop the final content of the split (which will always be Canada), preserving the elements at index -2 and -3 (the zip elements)

Excel - Pivot View Multiple Column Data without Expand/Collapse

I am trying to display multiple Columns of identical information without a Dropdown with the columns side by side and only appearing once.
My base information is as follows:
SKU Title Shipped
123-BJ00040-14 Betsey Johnson Women's BJ00040-14 2
123-BJ00131-29 Betsey Johnson Women's BJ00131-29 2
123-BJ00198-02 Betsey Johnson Women's BJ00198-02 2
123-BJ00131-29 Betsey Johnson Women's BJ00131-29 4
123-BJ00040-14 Betsey Johnson Women's BJ00040-14 3
In other words I have Row data with multiple columns of the same information for example the Column SKU always correlates to the Column Title. I am trying to view both the SKU and Title as non expand/collapse columns in the pivot and only SUM the Shipped Column so my pivot table should look like:
SKU Title Shipped
123-BJ00040-14 Betsey Johnson Women's BJ00040-14 5
123-BJ00131-29 Betsey Johnson Women's BJ00131-29 6
123-BJ00198-02 Betsey Johnson Women's BJ00198-02 2
Allowing me to copy the SKU, Title and Summed Shipped Qty together.

Reporting Row and Column Names Connected to a Value (Microsoft Access)

I'm well out of my depth on this one, as I don't use databases much. I hope it suffices that I've tried to pay it forward by helping people with InDesign and Photoshop on other websites!
I can use Access or Excel for what follows.
I have data that looks like:
year President Vice
1980 Reagan Bush Sr.
1984 Reagan Bush Sr.
1988 Bush Sr. Quayle
1992 Clinton Gore
1996 Clinton Gore
2000 Bush Jr. Cheney
2004 Bush Jr. Cheney
2008 Obama Biden
2012 Obama Biden
I want a report that looks like:
Biden: Vice 2008, 2012
Bush Jr.: President 2000, 2004
Bush Sr.: President 1988; Vice 1980, 1984
Cheney: Vice 2000, 2004
Clinton: President 1992, 1996
Gore: Vice 1992, 1996
Obama: President 2008, 2012
Quayle: Vice 1988
Reagan: President 1980, 1984
I'm having trouble figuring out how to identify a common name that may appear anywhere on the table, and how to grab the row and column labels for the report.
This is a simplified version of the real data, which doesn't have to do with politicians. There are actually ten column labels that are relevant, not just two. "Bush Sr." gives an example of one person holding two different offices.
There are not currently any cases where the same name appears in two different columns in the same row, but I'd prefer not to rule out the possibility, unless it's dramatically more complex to allow that.
Thanks!
The first thing we need to do is convert that data from "few rows, many columns" to "few columns, many rows" via a UNION query. (I saved your test data in a table called [N_column_table].)
SELECT [year], "President" AS office, [President] AS person
FROM [N_column_table]
UNION ALL
SELECT [year], "Vice" AS office, [Vice] AS person
FROM [N_column_table]
If you save that query as "3_column_data" then you can use it just like a table in other queries, reports, etc.. (You will have to add ~8 more UNION ALL constructs when you build your query for the real data.)
So now our data looks like this
year office person
1980 President Reagan
1984 President Reagan
1988 President Bush Sr.
1992 President Clinton
1996 President Clinton
2000 President Bush Jr.
2004 President Bush Jr.
2008 President Obama
2012 President Obama
1980 Vice Bush Sr.
1984 Vice Bush Sr.
1988 Vice Quayle
1992 Vice Gore
1996 Vice Gore
2000 Vice Cheney
2004 Vice Cheney
2008 Vice Biden
2012 Vice Biden
Now, as for "gluing together" the offices and years, we'll need to use a little VBA function for that. Create a Module in Access, and paste in the following code
Public Function ListTerms(person As String) As String
Dim cdb As DAO.Database
Dim rstOffice As DAO.Recordset, rstYear As DAO.Recordset
Dim result As String, yearString As String
Const YearSeparator = ", "
Const OfficeSeparator = "; "
Set cdb = CurrentDb
result = ""
Set rstOffice = cdb.OpenRecordset( _
"SELECT DISTINCT office " & _
"FROM 3_column_data " & _
"WHERE person=""" & Replace(person, """", """""", 1, -1, vbBinaryCompare) & """ " & _
"ORDER BY 1")
Do While Not rstOffice.EOF
yearString = ""
Set rstYear = cdb.OpenRecordset( _
"SELECT DISTINCT [year] " & _
"FROM 3_column_data " & _
"WHERE person=""" & Replace(person, """", """""", 1, -1, vbBinaryCompare) & """ " & _
"AND office=""" & Replace(rstOffice!Office, """", """""", 1, -1, vbBinaryCompare) & """ " & _
"ORDER BY 1")
Do While Not rstYear.EOF
If Len(yearString) > 0 Then
yearString = yearString & YearSeparator
End If
yearString = yearString & rstYear!Year
rstYear.MoveNext
Loop
rstYear.Close
Set rstYear = Nothing
If Len(result) > 0 Then
result = result & OfficeSeparator
End If
result = result & rstOffice!Office & " " & yearString
rstOffice.MoveNext
Loop
rstOffice.Close
Set rstOffice = Nothing
Set cdb = Nothing
ListTerms = result
End Function
Now we can use that function in a query to list each person and their terms in office
SELECT personlist.[person], ListTerms(personlist.[Person]) as terms
FROM (SELECT DISTINCT person FROM 3_column_data) personlist
which returns
person terms
Biden Vice 2008, 2012
Bush Jr. President 2000, 2004
Bush Sr. President 1988; Vice 1980, 1984
Cheney Vice 2000, 2004
Clinton President 1992, 1996
Gore Vice 1992, 1996
Obama President 2008, 2012
Quayle Vice 1988
Reagan President 1980, 1984

Resources